以前作った「組み込みRustハンズオンテキスト」
https://qiita.com/nanbuwks/items/5c2203f539575a58f29e
を元に、単なる動作確認。
環境
- Ubuntu 24.04 LTS
- 確認日 2026/6
事前準備
ディストリビューション版Rustをアンインストールしています。
$ sudo apt remove rustc
Rust 開発環境をインストール
https://rust-lang.org/learn/get-started/ にアクセスし、示されているコードをコピーします。

curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
これを実行すると、インストーラがダウンロードされてインストールされる。
info: downloading installer
warn: It looks like you have an existing rustup settings file at:
warn: /home/owner/.rustup/settings.toml
warn: Rustup will install the default toolchain as specified in the settings file,
warn: instead of the one inferred from the default host triple.
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/owner/.rustup
This can be modified with the RUSTUP_HOME environment variable.
The Cargo home directory is located at:
/home/owner/.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/owner/.cargo/bin
This path will then be added to your PATH environment variable by
modifying the profile files located at:
/home/owner/.profile
/home/owner/.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
>
- を選んで、開発環境をダウンロード/インストールします。
>1
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 2026-05-28 for version 1.96.0 (ac68faa20 2026-05-25)
info: downloading 6 components
cargo installed 10.64 MiB
clippy installed 4.58 MiB
rust-docs installed 21.29 MiB
rust-std installed 28.36 MiB
rustc installed 76.47 MiB
rustfmt installed 2.06 MiB
info: default toolchain set to stable-x86_64-unknown-linux-gnu
stable-x86_64-unknown-linux-gnu installed - rustc 1.96.0 (ac68faa20 2026-05-25)
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 "~/.cargo/env.nu" # For nushell
source "$HOME/.cargo/env.tcsh" # For tcsh
. "$HOME/.cargo/env.ps1" # For pwsh
source "$HOME/.cargo/env.xsh" # For xonsh
最後の表示のとおり、以下を実行して Rust ツールチェインのパスを通します。
$ . "$HOME/.cargo/env"
インストールされたものは以下の通りです。
$ ls ~/.cargo/bin
cargo cargo-fmt clippy-driver rust-analyzer rust-gdbgui rustc rustfmt
cargo-clippy cargo-miri rls rust-gdb rust-lldb rustdoc rustup
この時点で、作業マシンで実行してみます。
$ cargo new hello
Creating binary (application) `hello` package
note: see more `Cargo.toml` keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
cargo とは、Rust のパッケージマネージャおよびビルドツール
この時点で hello ディレクトリができていて、
$ ls hello* -alh
合計 68K
drwxrwxr-x 4 owner owner 4.0K 6月 8 19:07 .
drwxr-xr-x 49 owner owner 44K 6月 8 19:07 ..
drwxrwxr-x 6 owner owner 4.0K 6月 8 19:07 .git
-rw-rw-r-- 1 owner owner 8 6月 8 19:07 .gitignore
-rw-rw-r-- 1 owner owner 76 6月 8 19:07 Cargo.toml
drwxrwxr-x 2 owner owner 4.0K 6月 8 19:07 src
helloディレクトリに移って src を確認。
$ cd hello
$ ls -alh src
合計 12K
drwxrwxr-x 2 owner owner 4.0K 6月 8 19:07 .
drwxrwxr-x 4 owner owner 4.0K 6月 8 19:07 ..
-rw-rw-r-- 1 owner owner 45 6月 8 19:07 main.rs
$ cat src/main.rs
fn main() {
println!("Hello, world!");
}
もうプログラムソースができてしまっているので、そのままビルド。
$ cargo build
Compiling hello v0.1.0 (/home/owner/Downloads/hello)
Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.20s
$ ls -alh target/debug/
合計 4.2M
drwxrwxr-x 7 owner owner 4.0K 6月 8 19:11 .
drwxrwxr-x 3 owner owner 4.0K 6月 8 19:11 ..
-rw-rw-r-- 1 owner owner 0 6月 8 19:11 .cargo-artifact-lock
-rw-rw-r-- 1 owner owner 0 6月 8 19:11 .cargo-build-lock
-rw-rw-r-- 1 owner owner 0 6月 8 19:11 .cargo-lock
drwxrwxr-x 3 owner owner 4.0K 6月 8 19:11 .fingerprint
drwxrwxr-x 2 owner owner 4.0K 6月 8 19:11 build
drwxrwxr-x 2 owner owner 4.0K 6月 8 19:11 deps
drwxrwxr-x 2 owner owner 4.0K 6月 8 19:11 examples
-rwxrwxr-x 2 owner owner 4.2M 6月 8 19:11 hello
-rw-rw-r-- 1 owner owner 88 6月 8 19:11 hello.d
drwxrwxr-x 3 owner owner 4.0K 6月 8 19:11 incremental
hello は実行可能バイナリです。実行してみると
$ target/debug/hello
Hello, world!
無事成功しました。
なお、以下のようにすると、ビルドと実行がセットで行われる。
$ cargo run
クレートを試す
Rust のコンパイルに必要なひとかたまりがクレート? ライブラリやバイナリなどが含まれるらしい。
試しに、 rust-ansi-term というクレートを使ってみる。
ANSI端末でテキストの色を指定したりして装飾ができるようになるライブラリです。
作業ディレクトリに移って、
$ cargo new ansiterm
Creating binary (application) `ansiterm` package
note: see more `Cargo.toml` keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
$ cd ansiterm
$ vim Cargo.toml
として、
[package]
name = "ansiterm"
version = "0.1.0"
edition = "2024"
[dependencies]
となっているファイルの一番下に
ansi_term = "0.12"
を書き加えます。
0.12 はバージョンです。 https://crates.io/crates/ansi_term の記述どおりにしてみました。
$ vim src/main.rs
とすると、
fn main() {
println!("Hello, world!");
}
が出てきました。どうやらこのコードは常に配置されるようです。以下のように書き換えます。これも、先のサイトの記述を元にしています。
use ansi_term::Colour::Red;
fn main(){
println!("This is in red: {}", Red.paint("a red string"));
}
初回のビルドでは、ansi_termクロートが自動でダウンロードされます。
$ cargo build
Updating crates.io index
Locking 4 packages to latest Rust 1.96.0 compatible versions
Downloaded ansi_term v0.12.1
Downloaded 1 crate (24.3KiB) in 0.19s
Compiling ansi_term v0.12.1
Compiling ansiterm v0.1.0 (/home/owner/Downloads/rusttest/ansiterm)
Finished `dev` profile [unoptimized + debuginfo] target(s) in 1.56s
実行してみます。
$ target/debug/ansiterm
This is in red: a red string
"a red string" のところが赤く表示されました。
