安装
install
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
rustup -V
rustup component add rust-src
rustc -Vrustc 1.58.1 (db9d1b20b 2022-01-20
cargo -V
cargo 1.58.0 (f01b232bc 2022-01-19)
卸载
rustup self uninstall
ide
IntelliJ IDEA
https://plugins.jetbrains.com/plugin/8182-rust/docs/rust-quick-start.html
new project
cargo new start && cd start
.
├── Cargo.lock
├── Cargo.toml
├── src
│ └── main.rs
└── target
├── CACHEDIR.TAG
└── debug
├── build
├── deps
├── examples
├── incremental
├── start
└── start.d
本地文档
rustup doc
Rust 项目类型
主要分为两个类型:bin 和 lib,前者是一个可运行的项目,后者是一个依赖库项目。
编译项目 (debug +release模式)
cargo build
cargo build --release
运行项目
./target/debug/start
cargo run
cargo run --release
./target/release/start
更快的方式来验证代码的正确性
cargo check
Cargo.toml 和 Cargo.lock
Cargo.toml 是 cargo 特有的项目数据描述文件。它存储了项目的所有元配置信息,如果 Rust 开发者希望 Rust 项目能够按照期望的方式进行构建、测试和运行,那么,必须按照合理的方式构建 Cargo.toml。
Cargo.lock 文件是 cargo 工具根据同一项目的 toml 文件生成的项目依赖详细清单,因此我们一般不用修改它,只需要对着 Cargo.toml 文件撸就行了。
什么情况下该把 Cargo.lock 上传到 git 仓库里?很简单,当你的项目是一个可运行的程序时,就上传 Cargo.lock,如果是一个依赖库项目,那么请把它添加到 .gitignore 中
网友评论