RustCoderをやる

zenn.dev 上記を実施してみたので、その際のメモ書きを残す。

インストール

Rust をインストール - Rustプログラミング言語

私の場合はMacOSだったので、以下のコマンド。

$ curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh

インストールすると、シェルを再起動するか、sourceを実行するかを促される。

$ source "$HOME/.cargo/env"

ここまでできれば、インストールされていることを確認する。

❯ rustup --version
rustup 1.26.0 (5af9b9484 2023-04-05)
info: This is the version for the rustup toolchain manager, not the rustc compiler.
info: The currently active `rustc` version is `rustc 1.74.1 (a28077b28 2023-12-04)`

❯ rustc --version
rustc 1.74.1 (a28077b28 2023-12-04)

❯ cargo --version
cargo 1.74.1 (ecb9851af 2023-10-18)

cargo

パッケージの作成

❯ cargo new hello_world
     Created binary (application) `hello_world` package

パッケージを作成すると、指定した名称のディレクトリ(今回はhello_worldディレクトリ)が作成され、さらにその中にsrcディレクトリも作成される。
srcディレクトリには、main.rsというソースファイルも生成され、お馴染みのHello, worldプログラムが記述されている。

ビルド

❯ cargo build
   Compiling hello_world v0.1.0 (/Users/user/dev/hello_world)
    Finished dev [unoptimized + debuginfo] target(s) in 2.95s

実行

ビルドすると、新たにtargetディレクトリが作成され、その中に実行ファイルが作成される。

❯ target/debug/hello_world
Hello, world!

ビルドと実行を同時に

❯ cargo run
    Finished dev [unoptimized + debuginfo] target(s) in 0.00s
     Running `target/debug/hello_world`
Hello, world!

rust-analyzer

VSCodeの場合、rust-analyzerをインストールすると色々捗るみたい。

バージョン管理について

Atcoderでは各言語ごとに利用されているバージョンがあるので、自分の環境とAtcoderの環境を合わせる必要がある。
以下のコマンドを実行することで任意のRustバージョンを利用できる。この記事執筆時点では1.70.0がAtcoderの環境だったので、とりあえずそれに合わせておく。
(多分パッチバージョン程度であればずれていてもそこまで大きな問題は生じないとは思う)

❯ rustup toolchain install 1.70.0
❯ rustup default 1.70.0
❯ rustc --version
rustc 1.70.0 (90c541806 2023-05-31)

ちなみに、ローカルにインストールされているRustのバージョンは以下のコマンドで確認できる。

❯ rustup toolchain list
stable-x86_64-apple-darwin (default)
1.70.0-x86_64-apple-darwin

終わり

とりあえず、環境構築は何も問題なく終わり。特に何も問題なし。