メインコンテンツまでスキップ

Rust 環境構築

Install

rustup を用いて rust のインストールやバージョン管理を行います. rustup をインストールしたら $HOME/.cargo/bin を PATH に追加します.

sudo pacman -S rustup

rustup

rustup を用いて rustc などをアップデートする際には rustup update を実行します.

rustup update

rustup 自体をアップデートする場合にはディストリビューションのパケージマネージャでアップデートするか,rustup self update でアップデートします.

rustup self update

アンインストールは下記のコマンドで行います.

rustup self uninstall

cargo

新しいプロジェクトは cargo new で作成します.

cargo new dir_name --bin

git などのバージョン管理を行わない場合は --vcs none をつけます.

cargo new dir_name --bin --vcs none

ビルドは cargo build で行いますが,実行ファイルを作らなくていい場合は cargo check を実行します.

cargo build # create executable file
cargo check # don't create executable file

実行する場合は cargo run を使います.

cargo run

nightly 版の rust で実行したい場合は下記のように実行します.

rustup run nightly cargo run

rustc

rustc は rust のコンパイラで小さなコードであれば cargo を使わなくてもこれだけで対応できます.

rustc main.rs
./main

rustfmt

rustfmt main.rs