picoCTF 2025 の General Skills の 全5問のうち、3問が Rust を扱う問題でした。そこで、今回は、Rust の環境構築について記事にしておこうと思います。
それでは、やっていきます。
はじめに
Rust のソースコードは、拡張子が rs です。
コンパイル言語なので、Python などとは違い、C言語や Java に近いようです。
Rust の公式サイトは以下です。
また、日本語ドキュメントサイトがありました。ここに沿って、進めたいと思います。
以下の「Rust入門」も分かりやすかったです。
Rustの環境構築
公式サイトに沿って、進めていきたいと思います。最初に選択肢が出たので、デフォルトの標準インストールを選択しました。無事にインストールが完了したようです。
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh Welcome to Rust! This will download and install the official compiler for the Rust programming language, and its package manager, Cargo. Rustup metadata and toolchains will be installed into the Rustup home directory, located at: /home/user/.rustup This can be modified with the RUSTUP_HOME environment variable. The Cargo home directory is located at: /home/user/.cargo This can be modified with the CARGO_HOME environment variable. The cargo, rustc, rustup and other commands will be added to Cargo's bin directory, located at: /home/user/.cargo/bin This path will then be added to your PATH environment variable by modifying the profile files located at: /home/user/.profile /home/user/.bashrc You can uninstall at any time with rustup self uninstall and these changes will be reverted. Current installation options: default host triple: x86_64-unknown-linux-gnu default toolchain: stable (default) profile: default modify PATH variable: yes 1) Proceed with standard installation (default - just press enter) 2) Customize installation 3) Cancel installation info: profile set to 'default' info: default host triple is x86_64-unknown-linux-gnu info: syncing channel updates for 'stable-x86_64-unknown-linux-gnu' info: latest update on 2025-02-20, rust version 1.85.0 (4d91de4e4 2025-02-17) info: downloading component 'cargo' info: downloading component 'clippy' info: downloading component 'rust-docs' 18.2 MiB / 18.2 MiB (100 %) 10.6 MiB/s in 2s info: downloading component 'rust-std' 26.7 MiB / 26.7 MiB (100 %) 10.1 MiB/s in 2s info: downloading component 'rustc' 69.5 MiB / 69.5 MiB (100 %) 12.0 MiB/s in 6s info: downloading component 'rustfmt' info: installing component 'cargo' 8.7 MiB / 8.7 MiB (100 %) 2.8 MiB/s in 3s info: installing component 'clippy' 2.8 MiB / 2.8 MiB (100 %) 1.6 MiB/s in 1s info: installing component 'rust-docs' 18.2 MiB / 18.2 MiB (100 %) 889.6 KiB/s in 1m 28s info: installing component 'rust-std' 26.7 MiB / 26.7 MiB (100 %) 1.1 MiB/s in 22s info: installing component 'rustc' 69.5 MiB / 69.5 MiB (100 %) 635.2 KiB/s in 45s info: installing component 'rustfmt' 2.4 MiB / 2.4 MiB (100 %) 2.4 MiB/s in 1s info: default toolchain set to 'stable-x86_64-unknown-linux-gnu' stable-x86_64-unknown-linux-gnu installed - rustc 1.85.0 (4d91de4e4 2025-02-17) Rust is installed now. Great! To get started you may need to restart your current shell. This would reload your PATH environment variable to include Cargo's bin directory ($HOME/.cargo/bin). To configure your current shell, you need to source the corresponding env file under $HOME/.cargo. This is usually done by running one of the following (note the leading DOT): . "$HOME/.cargo/env" # For sh/bash/zsh/ash/dash/pdksh source "$HOME/.cargo/env.fish" # For fish source "$HOME/.cargo/env.nu" # For nushell
~/.bashrc を見ると、末尾に . "$HOME/.cargo/env" が追加されていました。読み込んで、バージョンを確認してみます。
環境構築できたようです。
$ source ~/.bashrc $ rustc -V rustc 1.85.0 (4d91de4e4 2025-02-17) $ cargo -V cargo 1.85.0 (d73d2caf9 2024-12-31)
Hello Worldをやってみる
日本語ドキュメントサイトの以下のページに沿って進めていきます。
まず、プロジェクトを作ります。Cargo はビルドシステム兼パッケージマネージャです。これを使ってみると、Hello World のソースコードが生成されました。
$ cargo new hello_world
Creating binary (application) `hello_world` package
note: see more `Cargo.toml` keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
$ cd hello_world/
$ tree --charset=C
.
|-- Cargo.toml
`-- src
`-- main.rs
2 directories, 2 files
$ cat Cargo.toml
[package]
name = "hello_world"
version = "0.1.0"
edition = "2024"
[dependencies]
$ cat src/main.rs
fn main() {
println!("Hello, world!");
}
早速ビルドします。たくさんのファイルが生成されました。実行してみると、無事に Hello World が表示されました。
$ cargo build Compiling hello_world v0.1.0 (/home/user/svn/experiment/rust/hello_world) Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.98s $ tree --charset=C . |-- Cargo.lock |-- Cargo.toml |-- src | `-- main.rs `-- target |-- CACHEDIR.TAG `-- debug |-- build |-- deps | |-- hello_world-0ebdf5153d0a7592 | `-- hello_world-0ebdf5153d0a7592.d |-- examples |-- hello_world |-- hello_world.d `-- incremental `-- hello_world-1t6fh3lk4md7i |-- s-h5b94a56sd-00mwz9a-aqu4sr9hmk5bq9mn320nz6aqe | |-- 0lmnvdafz0gu0v6qnyp9azcj5.o | |-- 3le850b0d6gihey3so9lye8y8.o | |-- 4nqyxhuu1l8523e9b2dkf370p.o | |-- 7o41adxcjqkn2cxacz69b6yr0.o | |-- c3ce73lb6w3yddy5uzj1zid9h.o | |-- dep-graph.bin | |-- dvay1ft8hqpvmntq9864bcjrf.o | |-- query-cache.bin | `-- work-products.bin `-- s-h5b94a56sd-00mwz9a.lock 10 directories, 18 files $ cat Cargo.lock # This file is automatically @generated by Cargo. # It is not intended for manual editing. version = 4 [[package]] name = "hello_world" version = "0.1.0" $ target/debug/hello_world Hello, world!
ビルドと実行を両方やってくれる cargo run もあります。
おわりに
今回は、picoCTF 2025 に参戦中に、Rust が必要になったので、導入記事ということで簡単に書きました。とりあえず、picoCTF 2025 は、上記の内容ぐらいで十分でした。必要に応じて、また追記しようと思います。
最後になりましたが、エンジニアグループのランキングに参加中です。
気楽にポチッとよろしくお願いいたします🙇
今回は以上です!
最後までお読みいただき、ありがとうございました。