Rustのrust-toolchain.tomlについて

Rustが楽しくてAWS SAAの資格勉強が全然捗りません。かなしい(自業自得) 前回のブログで軽く触れた、プロジェクトでRustを動かす際に役立つrust-toolchainファイルについて勉強がてら紹介していこうと思います。 Rustのバージョン管理について プロジェクトのバージョン管理はそれぞれの言語で違います。 (nodeであればnodenv、pythonであればpyenvなど…) Rustの場合、使用するバージョンをプロジェクトごとに決める際は、rust-toolchain.tomlが役に立ちます。 Overrides - The rustup book これにより、各プロジェクトごとにRustのバージョンを切り替える必要がなくなります。 使用するためにはrustupのバージョンを1.23.0以上にしてあげる必要があります。 rust-toolchain.tomlの形式は以下のとおりです。 [toolchain] channel = "nightly-2023-08-03" components = [ "rustfmt", "rustc-dev" ] targets = [ "wasm32-unknown-unknown", "thumbv2-none-eabi" ] profile = "minimal" バージョンだけ指定するならchannelだけ指定してあげればいいです。 [toolchain] channel = "nightly-2023-08-03" これをCargoパッケージ配下に配置することで、バージョンの指定が可能になります。 もしインストールされていないバージョンが指定されていた場合、cargo buildなどを行うと以下のように自動でバージョンのインストールを行ってくれます。 以下はnightly-2023-08-03をインストールしていない状態でchannelにnightly-2023-08-03を指定し、cargo testを実行した際の出力です。 % cargo test info: syncing channel updates for 'nightly-2023-08-03-x86_64-unknown-linux-gnu' info: latest update on 2023-08-03, rust version 1.73.0-nightly (8131b9774 2023-08-02) info: downloading component 'cargo' info: downloading component 'clippy' info: downloading component 'rust-docs' info: downloading component 'rust-std' info: downloading component 'rustc' info: downloading component 'rustfmt' info: installing component 'cargo' info: installing component 'clippy' info: installing component 'rust-docs' info: installing component 'rust-std' info: installing component 'rustc' info: installing component 'rustfmt' Compiling helper v0.1.0 (/home/username/Project/study/rust-lib-and-bin-playground/helper) Finished test [unoptimized + debuginfo] target(s) in 0.72s Running unittests src/lib.rs (target/debug/deps/helper-bf168264d06a5721) running 2 tests test test::bench_add_two ... ok test test::test_add ... ok test result: ok. 2 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s Doc-tests helper running 0 tests test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s きちんとインストールされますね! ...

2023年8月5日 · にあえん

【Rust】Cargoターゲットの使い分け方

やっと…仕事でRustを触れるようになりました!!! 嬉しい!! とはいえ、Rustでたまに実装しているものの、基本pythonでしか実装していない日々だったので、どういうルールがあったのかを忘れてしまいました。。 まずはCargoターゲットについて学習していきたいと思い、その備忘録になります。 CargoパッケージとCargoターゲットとは Rustでプロジェクトを作るときはまずcargo new [プロジェクト名]のように、cargoを利用してプロジェクトの基盤をまず作ると思います。 ここで作られたプロジェクトのことは「Cargoパッケージ」と言います。 Cargoパッケージには以下に分類されるコードを含めることができます。 library binary example test benchmark Cargo Targets - The Cargo Book library ライブラリは、Cargoパッケージから参照される想定のコードのことです。 CLIのようなバイナリとして実行するアプリケーションではなく、他のRustライブラリが参照することを想定して実装する場合はこちらを使います。 binary 文字通りバイナリとしてビルドし実行する想定のコードのことです。 こちらはライブラリのように他のRustライブラリが参照するというよりは、単体で動作するソフトウェアを作るのに役立ちます。 example 主にライブラリの使用例を表すコードのことです。 cargoの各コマンドで使用例を使うためのサポートがされています。 You can run individual executable examples with the cargo run command with the –example option. Library examples can be built with cargo build with the –example option. cargo install with the –example option can be used to copy executable binaries to a common location. ...

2023年8月3日 · にあえん