一、安装
1 mac 、Linux 、another Unix-like os
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs |sh
1 stable 最稳定和可靠的版本
2 Nightly 每天构建的最新版本
安装 rustup install stable/nightly
切换 rustup default stable/nightly
二、命令操作
1 更新Rust
rustup update
2 卸载
rustup self uninstall
3 添加组建
rustup component add rustfmt
4 查看版本
rustup --version
三、Rust 编程语言的编译器rustc
1 查看版本
rustc --version
2 编译生成二进制文件
rustc -o output_filename filename.rs
3 编译生成库文件
rustc --crate-type lib filename.rs
四、Rust 的包管理工具Cargo
1 隐式地使用rustc 进行编译
2 命令
- 创建
cargo new project_name
目录结构
cargo new --lib project_name
- 构建项目(生成二进制可执行文件或库文件)
cargo build
为生成优化的可执行文件,常用于生产环境
cargo build --release
- 检测
cargo check
- 运行/测试(会隐式的使用这个Rust进行编译)
cargo run/cargo test
3 Cargo.toml 文件
- package
设置项目名
版本
- dependencies
设置依赖
[build-dependencies]列出了在构建项目是需要的依赖项
[dev-dependencies] 列出了只在开发时需要的依赖项
五、Cargo 插件 cargo-edit
1 安装
cargo install cargo-edit
2 添加库
cargo add dependency_name
3 安装指定版本
cargo add dependency_name@1.2.3
4 添加开发时用的依赖库
cargo add --dev dev_dependency_name
5 添加构建时用的依赖库
cargo add --build_dependency_na-me
6 删除库
cargo rm dependency_name