Rust 环境配置
本章将介绍如何安装 Rust 和配置开发环境。
安装 Rust
使用 rustup(推荐)
rustup 是 Rust 官方的工具链管理器,可以方便地安装和管理 Rust 版本。
Linux 和 macOS
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
安装完成后,重启终端或运行:
source $HOME/.cargo/env
Windows
- 下载 rustup-init.exe
- 运行安装程序,按照提示完成安装
- 如果没有安装 Visual Studio Build Tools,安装程序会提示安装
验证安装
rustc --version
cargo --version
输出示例:
rustc 1.75.0 (82e1608df 2023-12-21)
cargo 1.75.0 (1d8b05cdd 2023-11-20)
手动安装
如果无法使用 rustup,可以从 Rust 官网 下载对应平台的安装包。
rustup 常用命令
# 查看已安装的工具链
rustup show
# 更新到最新版本
rustup update
# 安装特定版本
rustup install 1.75.0
# 设置默认工具链
rustup default stable
# 安装 nightly 版本
rustup install nightly
rustup default nightly
# 查看文档
rustup doc
rustup doc --book # 打开 The Book
# 添加目标平台(交叉编译)
rustup target add x86_64-pc-windows-gnu
Cargo 包管理器
Cargo 是 Rust 的构建系统和包管理器,它会随 Rust 一起安装。
创建项目
# 创建新项目
cargo new my_project
# 创建库项目
cargo new my_library --lib
# 在现有目录初始化
cargo init
创建的项目结构:
my_project/
├── Cargo.toml # 项目配置文件
├── .gitignore
└── src/
└── main.rs # 主程序入口
Cargo.toml 配置
[package]
name = "my_project" # 项目名称
version = "0.1.0" # 版本号
edition = "2021" # Rust Edition
authors = ["Your Name <[email protected]>"]
description = "A sample project"
license = "MIT"
[dependencies] # 依赖项
serde = "1.0" # 来自 crates.io
regex = "1.5" # 版本号
rand = { version = "0.8", features = ["small_rng"] } # 带特性
[dev-dependencies] # 开发依赖
criterion = "0.3"
[build-dependencies] # 构建依赖
cc = "1.0"
[profile.release] # 发布优化配置
opt-level = 3 # 优化级别
lto = true # 链接时优化
构建和运行
# 检查代码(快速,不生成可执行文件)
cargo check
# 构建项目
cargo build
# 构建 release 版本(优化)
cargo build --release
# 运行项目
cargo run
# 运行 release 版本
cargo run --release
# 运行测试
cargo test
# 生成文档
cargo doc
# 生成并打开文档
cargo doc --open
依赖管理
# 添加依赖
cargo add serde
cargo add serde --features derive
cargo add tokio --features full
# 添加开发依赖
cargo add --dev criterion
# 移除依赖
cargo rm serde
# 更新依赖
cargo update
# 查看依赖树
cargo tree
# 检查过时的依赖
cargo outdated # 需要安装: cargo install cargo-outdated
常用 Cargo 工具
# 安装二进制 crate
cargo install ripgrep
cargo install bat
cargo install exa
# 格式化代码
cargo fmt
# 代码检查(更严格)
cargo clippy
# 安全审计
cargo audit # 需要安装: cargo install cargo-audit
开发工具配置
VS Code
推荐扩展:
- rust-analyzer:官方 Rust 语言服务器,提供代码补全、类型提示等
- CodeLLDB:调试支持
- Better TOML:Cargo.toml 语法高亮
- crates:依赖版本提示
配置 settings.json
{
"rust-analyzer.checkOnSave.command": "clippy",
"rust-analyzer.cargo.features": "all",
"[rust]": {
"editor.defaultFormatter": "rust-lang.rust-analyzer",
"editor.formatOnSave": true
}
}
其他 IDE
- IntelliJ IDEA:安装 Rust 插件
- Vim/Neovim:使用 rust-analyzer LSP
- Emacs:使用 rust-mode 和 rustic
Rust 项目结构
典型的项目结构
my_project/
├── Cargo.toml
├── Cargo.lock # 依赖锁定文件(自动生成)
├── src/
│ ├── main.rs # 二进制入口
│ ├── lib.rs # 库入口
│ └── module/
│ └── mod.rs # 子模块
├── tests/ # 集成测试
│ └── integration_test.rs
├── benches/ # 性能测试
│ └── benchmark.rs
├── examples/ # 示例代码
│ └── example.rs
└── docs/ # 文档
└── some_doc.md
Workspace(工作空间)
对于多 crate 项目:
# 根目录的 Cargo.toml
[workspace]
members = [
"crates/api",
"crates/core",
"crates/utils",
]
常见问题
1. 编译速度慢
# 使用更快的链接器
# 在 ~/.cargo/config.toml 中添加
[target.x86_64-unknown-linux-gnu]
linker = "clang"
rustflags = ["-C", "link-arg=-fuse-ld=lld"]
# 使用 sccache 缓存编译结果
cargo install sccache
export RUSTC_WRAPPER=sccache
2. 镜像源配置
在中国大陆,可以配置镜像源加速下载:
# ~/.cargo/config.toml
[source.crates-io]
replace-with = 'ustc'
[source.ustc]
registry = "https://mirrors.ustc.edu.cn/crates.io-index"
[source.tuna]
registry = "https://mirrors.tuna.tsinghua.edu.cn/git/crates.io-index.git"
3. 交叉编译
# 添加目标平台
rustup target add x86_64-pc-windows-gnu
# 编译
cargo build --target x86_64-pc-windows-gnu
4. 更新 Rust 版本
# 更新 rustup 和 Rust
rustup update
# 更新到特定版本
rustup update 1.75.0
小结
本章我们学习了:
- Rust 的安装方法(rustup)
- Cargo 包管理器的基本使用
- Cargo.toml 配置文件结构
- 常用的 Cargo 命令
- VS Code 开发环境配置
- Rust 项目结构
- 常见问题的解决方案
练习
- 安装 Rust 并验证安装成功
- 使用 cargo new 创建一个新项目
- 添加一个依赖并运行 cargo build
- 使用 cargo doc 生成文档
- 配置 VS Code 和 rust-analyzer