1、創建rust工作區域:
cargo new hello-rust // cargo new 爲使用rust 構建工具和包管理器 cargo 新建工作區
2、工作區層次與介紹:
hello-rust
|- Cargo.toml
|- src
|- main.rs
hello-rust 爲所創建的工作區名
。
Cargo.toml
爲 Rust 的清單文件。其中包含了項目的元數據和依賴庫。
src/main.rs
爲編寫應用代碼的地方。
3、hello-rust.rs 程序:
fn main() {
let a = 12;
let b = "Hello World!";
let c = "JJ";
println!("a is {}", a); // rust中使用{}替代C語言的%d格式化輸出
println!("a is {},a is {}", a,a); // 輸出兩次
println!("a is {0},a is {0}", a); // 輸出兩次優化
println!("{},{}",b,c);
println!("{{}}"); // 除了{}外其他的轉義字符與c語言一樣
}
4、cargo 其他指令:
cargo run
可以運行項目cargo build
可以構建項目cargo test
可以測試項目cargo doc
可以爲項目構建文檔cargo publish
可以將庫發佈到 crates.io-
cargo --version 查看
是否安裝了 Rust 和 Cargo