Day 4:第一個專案(hello-rust)

學習目標

  • 用 cargo 建立第一個專案
  • 看懂 cargo new 產生的檔案
  • 能執行並修改你的第一個 Rust 程式

今日重點

建立專案

cargo new hello-rust
cd hello-rust
cargo run
輸出會是:
   Compiling hello-rust v0.1.0 (...)
    Finished ...
     Running `target/debug/hello-rust`
Hello, world!

修改你的第一個程式

編輯 src/main.rs

fn main() {
    println!("Hello, Rust!");
    println!("2 + 3 = {}", 2 + 3);
}
再跑一次 cargo run,看看輸出。

練習

  1. 建立 hello-rust 專案並成功 cargo run
  2. main.rs 裡用 println! 印出你自己的名字。
  3. 印出 10 * 5 的結果(提示:println!("{}", 10 * 5);)。

自我檢查

  • 能建立新專案(cargo new)
  • 能執行專案(cargo run)
  • 能修改 main.rs 並看到新的輸出
  • 知道 {} 在 println! 裡代表「插入值的位置」