環境
- Windows 10
- Rust 1.54.0
Hello World
根據傳統,首先編寫一個 Hello World 程序。
參考這里的代碼:https://doc.rust-lang.org/cargo/getting-started/first-steps.html
Cargo 是 Rust 的包管理器,和 Java 中的 Maven 類似。
建立目錄結構
首先建立一個項目目錄:hello_world,然后在目錄中新建 Cargo.toml 和 src 文件夾,
最后在 src 文件夾下新建一個 main.rs 的文件,目錄結構如下:
├── Cargo.toml
└── src
└── main.rs
main.rs 內容
fn main() {
println!("Hello World");
}
Cargo.toml 內容
[package]
name = "hello_world"
version = "0.1.0"
edition = "2018"
[dependencies]
編譯源代碼
編譯好之后,默認在 target/debug/
目錄下生成了一個可執行的二進制文件:hello_world.exe。
C:\Users\jiangbo\work\workspace\rust\hello_world>cargo build
Compiling hello_world v0.1.0 (C:\Users\jiangbo\work\workspace\rust\hello_world)
Finished dev [unoptimized + debuginfo] target(s) in 1.50s
運行程序
C:\Users\jiangbo\work\workspace\rust\hello_world>target\debug\hello_world.exe
Hello World
使用 Cargo 運行程序
C:\Users\jiangbo\work\workspace\rust\hello_world>cargo run
Finished dev [unoptimized + debuginfo] target(s) in 0.01s
Running `target\debug\hello_world.exe`
Hello World
總結
編寫了一個 Rust 程序,使用了 Cargo 的目錄結構,並且進行了編譯和運行。