Rust の最初のステップ - Learn
- Rust は rustup と rust-analyzer を homebrew でインストール
- bootstrap で .cargo フォルダがないことを確認の上で rustup-init を実行
- vscode の Rust 機能拡張を追加
"[rust]": {
"editor.formatOnSave": true,
},
fn main() {
println!("Hello, world!");
}
- rustc でコンパイルして実行。これは cc と変わらない
rustc main.rs
./main
- でも実際のプロジェクトはこんな簡単ではないので、cargo を使う。ビルドツールであり、環境依存マネージャーでもあるとのこと。cargo new でプロジェクトを作成
cargo new hello-cargo
- 設定などは Cargo.toml で記述。toml ファイルは vim 以来か。cargo run でプログラム実行
cargo run
Rust の一般的な概念を理解する - Learn
Rust プログラム構造の概要 - Learn
- 関数について。関数は fn で記述。Ruby の def と同じ感じ。行は ; で終わるが、終わらない時は次の行を 4 インデントする。このあたりはフォーマッタがなんとかしてくれると信じる
fn main() { // The function declaration is not indented
// First step in function body
// Substep: execute before First step can be complete
// Second step in function body
// Substep A: execute before Second step can be complete
// Substep B: execute before Second step can be complete
// Sub-substep 1: execute before Substep B can be complete
// Third step in function body, and so on...
}
- println! はマクロ。{} は C 言語の %s みたいな感じ。数字などが渡されても文字列変換メソッドが入るので、型を区別しなくていいということ。