Rustで動かす最小の「Hello World」

コンピュータ

Rust は近年 CLIツール、WebAssembly、システム開発で注目を集めています。
今回は、Windows で Rust をインストールし、最小の Hello World を動かすところまでやってみます。


Scoopで Rust をインストールする

・Scoop が未インストールの場合

Set-ExecutionPolicy RemoteSigned -scope CurrentUser
iwr -useb get.scoop.sh | iex

・Rust のインストール

scoop install rustup

・動作確認

rustc --version
rustc 1.91.1 (ed61e7d7e 2025-11-07)
cargo --version
cargo 1.91.1 (ea2d97820 2025-10-10)
rustup --version
rustup 1.28.2 (e4f3ad6f8 2025-04-28)

インストールできたようです。

プロジェクトを作成する

Rust には cargo というプロジェクト生成とビルドを兼ねたツールが付属しています。

プロジェクト生成:

cargo new "j:\rust\rust-hello"
cd rust-hello
フォルダー パスの一覧:  ボリューム WORKSPACE_WD
ボリューム シリアル番号は 5695-DEC8 です
J:\RUST\RUST-HELLO
|   .gitignore
|   Cargo.toml
|   
\---src
        main.rs

Cargo.toml を確認する

自動作成されたCargo.tomlは以下のような内容でした。
ファイル名:Cargo.toml

[package]
name = "rust-hello"
version = "0.1.0"
edition = "2024"

[dependencies]

Hello World を書く

ソースコードを書くまでもなく、生成されたコードがHello worldでした。
ファイル名:src/main.rs

fn main() {
    println!("Hello, world!");
}

コンパイル & 実行

Rustはコンパイル言語なので、ビルドして実行します。

cargo run

   Compiling rust-hello v0.1.0 (J:\rust\rust-hello)
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.68s
     Running `target\debug\rust-hello.exe`
Hello, world!

Hello, world!が表示されました。

ビルド成果物の確認

 ls target\debug

    Directory: J:\rust\rust-hello\target\debug

Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
d----          2025/12/06    17:11                .fingerprint
d----          2025/12/06    17:11                build
d----          2025/12/06    17:11                deps
d----          2025/12/06    17:11                examples
d----          2025/12/06    17:11                incremental
-a---          2025/12/06    17:11              0 .cargo-lock
-a---          2025/12/06    17:11        1339392 rust_hello.pdb
-a---          2025/12/06    17:11             79 rust-hello.d
-a---          2025/12/06    17:11         137216 rust-hello.exe

“rust-hello.exe”が作成されています。

直接実行:

target\debug\rust-hello.exe
Hello, world!

感想

学習難易度が高いと言われるRustですが、Hello worldであれば、流石に他の言語と変わらない感じです。
今後学習を進めるか未定ですが、環境が出来ているので、いつでも始めることが出来ると思います。

コメント