はじめに
これはただのメモです。RUSTについてhello world しました。
内容
公式サイトをみてインストールとhello world しました。
日本語の訳のページもありました。
インストール
WSL2, Ubuntu24.04 を利用しています。書かれている通り。標準設定でそのままで入りました。
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
丁寧にメッセージが表示されます。
/home/xt/.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/xt/.cargo/bin
This path will then be added to your PATH environment variable by
modifying the profile files located at:/home/xt/.profile
/home/xt/.bashrcYou 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
- Proceed with standard installation (default - just press enter)
- Customize installation
- Cancel installation
略
stable-x86_64-unknown-linux-gnu installed - rustc 1.90.0 (1159e78c4 2025-09-14)
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
いろいろなRUSTのツールがインストールされました。更新しましたが、最新版が入っていました。
rustup update
コンパイラのバージョンの確認。
$ rustc --version
rustc 1.90.0 (1159e78c4 2025-09-14)
Hello world
ソースを書いてコンパイル
fn main() {
println!("Hello, world!");
}
コンパイルして実行。なんだか動きが早い気がしました。
$ rustc main.rs
$ ./main
Hello, world!
管理ツールを使う
こちらが本命でしょう。cargo というツールを使うといろいろできるらしい。
- build your project with cargo build
- run your project with cargo run
- test your project with cargo test
- build documentation for your project with cargo doc
- publish a library to crates.io with cargo publish
下記コマンドでプロジェクトを作ります。Carg.toml というファイルが作られます。
$ cargo new hello-rust
$ cd hello-rust
$ tree
.
├── Cargo.toml
└── src
└── main.rs
2 directories, 2 files
実行はrun です。
$ cargo run
Compiling hello v0.1.0 (/home/xtkd/torupati/learning_rust/projects/hoge/hello)
Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.32s
Running `target/debug/hello`
Hello, world!
Dependencies の追加
Rustの世界ではpackages を "crates" と呼ぶとのこと。こちらに使えるもののリストがある。
https://crates.io/
追加するには、Cargo.toml に追記する。(コマンドラインでもできないのな?)
[package]
name = "hello-rust"
version = "0.1.0"
edition = "2024"
[dependencies]
ferris-says = "0.3.1"
コードでは、use というのを使ってmodule を import するらしい。
use ferris_says::say; // from the previous step
use std::io::{stdout, BufWriter};
fn main() {
let stdout = stdout();
let message = String::from("Hello fellow Rustaceans!");
let width = message.chars().count();
let mut writer = BufWriter::new(stdout.lock());
say(&message, width, &mut writer).unwrap();
}
そして、cargo build, cargo run で実行できます。
$ cargo run main
Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.01s
Running `target/debug/hello-rust main`
__________________________
< Hello fellow Rustaceans! >
--------------------------
\
\
_~^~^~_
\) / o o \ (/
'_ - _'
/ '-----' \
ファイルは target 以下に作られるようです。下記はいろいろ実行した後の状態です。
$ tree -L 2 .
.
├── Cargo.lock
├── Cargo.toml
├── src
│ └── main.rs
└── target
├── CACHEDIR.TAG
├── debug
└── doc
5 directories, 4 files
中身をみると拡張子が.o *.d のファイルなど、いろいろありました。
$ ls target/debug/
build deps examples hello-rust hello-rust.d incremental
cargo doc を実行すると、target/doc にファイルが作られました。また、cargo test では何かをテストしてくれたようです。
$ cargo test
Compiling hello-rust v0.1.0 (/home/xt/learning_rust/projects/hello-rust)
Finished `test` profile [unoptimized + debuginfo] target(s) in 0.38s
Running unittests src/main.rs (/home/xt/learning_rust/projects/hello-rust/target/debug/deps/hello_rust-15b35d31b08f6cc4)
running 0 tests
test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s
参考サイト
このZennのサイトが良さそうです。そこで紹介されていましたが、
まとめ
とりあえず、インストールとhello world ができた。いくつか処理を書いてみたいと思います。
機械学習アルゴリズムを実装してみるかな。
(2025/10/15)