iT邦幫忙

3

二、三天學一點點 Rust:來!先從建立專案開始(1)

  • 分享至 

  • xImage
  •  

全程使用VScode編譯。
先在桌面建立一個資料Rust,以便統一管理。
進入VScode之後,再終端機輸入:

C:\Users\iamad\Desktop\Rust> cargo new about_you

建立一個新的 Rust 專案,專案名稱是 about_you。
會自動幫你建立資料夾結構,在VScode左手邊會看到:例如 src/main.rs、Cargo.toml 等。

C:\Users\iamad\Desktop\Rust> cd about_you 
C:\Users\iamad\Desktop\Rust\about_you> cd src      
C:\Users\iamad\Desktop\Rust\about_you\src> ls 

Directory: C:\Users\iamad\Desktop\Rust\about_you\src 

Mode                 LastWriteTime         Length  Name   
----                 -------------         ------  ----   
-a---          2025/4/8 下午 11:47            120  main.rs

切換到新建立的專案目錄與原始碼目錄 src。
列出目前目錄下的檔案,這裡顯示的是預設的 main.rs。

fn main() {
    println!("Hello, My love.");
    println!("Your name is Lei.");
    println!("You are so beautiful.");
}

其中,main.rs的內容如上,println!後方原本是Hello, World!

PS C:\Users\iamad\Desktop\Rust\about_you\src> rustc main.rs
PS C:\Users\iamad\Desktop\Rust\about_you\src> ls

  Directory: C:\Users\iamad\Desktop\Rust\about_you\src  

Mode                 LastWriteTime         Length Name    
----                 -------------         ------ ----    
-a---          2025/4/8 下午 11:50         144384 main.exe
-a---          2025/4/8 下午 11:50        1282048 main.pdb
-a---          2025/4/8 下午 11:47            120 main.rs

直接用 Rust 編譯器(rustc)編譯這個檔案,會產生:
main.exe(Windows 上的可執行檔)
main.pdb(除錯資訊) 這是「手動編譯」,不是用 Cargo。
左邊檔案總管也會顯示,此處特意用ls,只是提醒自己一些終端機指令的用法。

PS C:\Users\iamad\Desktop\Rust\about_you\src> .\main.exe
Hello, My love.
Your name is Lei.
You are so beautiful.

執行剛才手動編譯出來的程式,結果印出如上。
.\main.exe,這是Window寫法。我是在Window作業的。
./main為Mac或是Linux寫法,如果你有安裝 PowerShell,兩者皆可用。

PS C:\Users\iamad\Desktop\Rust\about_you\src> cd .. 
PS C:\Users\iamad\Desktop\Rust\about_you> cargo build
    Finished dev profile [unoptimized + debuginfo] target(s) in 0.01s

用 Cargo(Rust 的建構系統)來編譯整個專案,預設是 debug 模式。
產生的可執行檔會放在 target/debug/about_you.exe。

PS C:\Users\iamad\Desktop\Rust\about_you> ./target/debug/about_you
Hello, My love.
Your name is Lei.
You are so beautiful.

執行剛才用 Cargo 編譯出來的可執行檔。

PS C:\Users\iamad\Desktop\Rust\about_you> cargo run  
    Finished dev profile [unoptimized + debuginfo] target(s) in 0.01s
     Running target\debug\about_you.exe
Hello, My love.
Your name is Lei.
You're so beautiful.

cargo run 等同於

cargo build
./target/debug/about_you

主要是開發中頻繁測試結果。如果程式沒改,Cargo 會直接執行,不會重編。

PS C:\Users\iamad\Desktop\Rust\about_you> cargo clean
     Removed 43 files, 3.0MiB total

清除整個 target/目錄,也就是所有編譯產物都會被刪除(debug 和 release 都會)。
這動作主要是長時間開發之下,target/目錄下會累積很多暫存檔(debug symbols、release 產物),又或者版本更新、相依套件改變或手動過編譯檔案,會導致編譯出現奇怪錯誤,cargo clean 可以確保重新「乾淨地編譯一次」。

PS C:\Users\iamad\Desktop\Rust\about_you> cargo run
   Compiling about_you v0.1.0 (C:\Users\iamad\Desktop\Rust\about_you)
    Finished dev profile [unoptimized + debuginfo] target(s) in 0.30s
     Running target\debug\about_you.exe
Hello, My love.
Your name is Lei.
You're so beautiful.

因為剛才清空debug等資料夾,此時再執行一次cargo run,target資料夾又會再度出現,至於原因前述已說明,這裡可以直接用cargo build --release直接優化檔案。

PS C:\Users\iamad\Desktop\Rust\about_you> cargo check
    Checking about_you v0.1.0 (C:\Users\iamad\Desktop\Rust\about_you)
    Finished dev profile [unoptimized + debuginfo] target(s) in 0.06s

只檢查程式碼是否能成功編譯(語法正確與型別正確),但不產生執行檔。
適合快速檢查錯誤,速度很快。

PS C:\Users\iamad\Desktop\Rust\about_you> cargo fmt

自動格式化你的 Rust 程式碼(像 main.rs),讓程式風格統一、美觀。
不會影響邏輯,只是排版。

剛開始練習的時候,有種這兩個指令好像?,例如cargo build、cargo run,加上又是
英文課程,所以我假設英文不好的我,可能老師有說但我漏掉了,還好現在有GPT輔助。


尚未有邦友留言

立即登入留言