Rust 环境配置
本章节将指导你配置 Rust 开发环境,包括 Rust 安装、包管理、开发工具配置等。
📋 配置清单
- Rust 安装
- 镜像配置
- 开发工具安装
- 项目配置
- 验证安装
1. 安装 Rust
使用 rustup 安装
macOS/Linux
bash
# 安装 Rust
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | shbash
# 重新加载配置
source ~/.cargo/envWindows
powershell
# 安装 Rust
# 访问: https://rustup.rs/
# 下载并运行 rustup-init.exe验证安装
macOS/Linux
bash
# 检查 Rust 版本
rustc --versionbash
# 检查 Cargo 版本
cargo --versionWindows
powershell
# 检查 Rust 版本
rustc --versionpowershell
# 检查 Cargo 版本
cargo --version2. 配置 Rust 镜像
配置 Cargo 镜像
macOS/Linux
bash
# 创建 Cargo 配置目录
mkdir -p ~/.cargobash
# 配置镜像
echo '[source.crates-io]
registry = "https://github.com/rust-lang/crates.io-index"
replace-with = "ustc"
[source.ustc]
registry = "git://mirrors.ustc.edu.cn/crates.io-index"' >> ~/.cargo/configWindows
powershell
# 创建 Cargo 配置目录
New-Item -Path ~/.cargo -ItemType Directory -Forcepowershell
# 创建配置文件
New-Item -Path ~/.cargo/config -ItemType File -Forcepowershell
# 配置镜像
@"
[source.crates-io]
registry = "https://github.com/rust-lang/crates.io-index"
replace-with = "ustc"
[source.ustc]
registry = "git://mirrors.ustc.edu.cn/crates.io-index"
"@ | Out-File -FilePath ~/.cargo/config -Encoding UTF83. 安装开发工具
安装 Rust 组件
macOS/Linux
bash
# 安装 rust-analyzer (语言服务器)
rustup component add rust-analyzerbash
# 安装 clippy (代码检查工具)
rustup component add clippybash
# 安装 rustfmt (代码格式化工具)
rustup component add rustfmtbash
# 安装 cargo-audit (安全审计工具)
cargo install cargo-auditWindows
powershell
# 安装 rust-analyzer (语言服务器)
rustup component add rust-analyzerpowershell
# 安装 clippy (代码检查工具)
rustup component add clippypowershell
# 安装 rustfmt (代码格式化工具)
rustup component add rustfmtpowershell
# 安装 cargo-audit (安全审计工具)
cargo install cargo-audit安装常用工具
macOS/Linux
bash
# 安装 cargo-watch (文件监控)
cargo install cargo-watchbash
# 安装 cargo-expand (宏展开)
cargo install cargo-expandbash
# 安装 cargo-tree (依赖树)
cargo install cargo-treeWindows
powershell
# 安装 cargo-watch (文件监控)
cargo install cargo-watchpowershell
# 安装 cargo-expand (宏展开)
cargo install cargo-expandpowershell
# 安装 cargo-tree (依赖树)
cargo install cargo-tree4. 项目配置
创建新项目
macOS/Linux
bash
# 创建二进制项目
cargo new myproject
cd myprojectbash
# 创建库项目
cargo new mylib --lib
cd mylibWindows
powershell
# 创建二进制项目
cargo new myproject
cd myprojectpowershell
# 创建库项目
cargo new mylib --lib
cd mylib项目操作
macOS/Linux
bash
# 构建项目
cargo buildbash
# 运行项目
cargo runbash
# 运行测试
cargo testbash
# 检查代码
cargo checkbash
# 格式化代码
cargo fmtbash
# 代码检查
cargo clippyWindows
powershell
# 构建项目
cargo buildpowershell
# 运行项目
cargo runpowershell
# 运行测试
cargo testpowershell
# 检查代码
cargo checkpowershell
# 格式化代码
cargo fmtpowershell
# 代码检查
cargo clippy5. 配置文件
创建 .gitignore
macOS/Linux
bash
# 创建 .gitignore 文件
touch .gitignorebash
# 添加 Rust 相关忽略规则
echo "# Generated by Cargo
/target/
# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html
Cargo.lock
# These are backup files generated by rustfmt
**/*.rs.bk
# MSVC Windows builds of rustc generate these, which store debugging information
*.pdb
# IDE files
.vscode/
.idea/
*.swp
*.swo" > .gitignoreWindows
powershell
# 创建 .gitignore 文件
New-Item -Path .gitignore -ItemType Filepowershell
# 添加 Rust 相关忽略规则
@"
# Generated by Cargo
/target/
# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html
Cargo.lock
# These are backup files generated by rustfmt
**/*.rs.bk
# MSVC Windows builds of rustc generate these, which store debugging information
*.pdb
# IDE files
.vscode/
.idea/
*.swp
*.swo
"@ | Out-File -FilePath .gitignore -Encoding UTF86. 调试配置
VS Code 调试配置
创建 .vscode/launch.json 文件:
json
{
"version": "0.2.0",
"configurations": [
{
"type": "lldb",
"request": "launch",
"name": "Debug executable",
"cargo": {
"args": ["build", "--bin=myproject"],
"filter": {
"name": "myproject",
"kind": "bin"
}
},
"args": [],
"cwd": "${workspaceFolder}"
}
]
}7. 性能优化
配置 Cargo 缓存
macOS/Linux
bash
# 查看 Cargo 缓存位置
cargo --versionbash
# 清理 Cargo 缓存
cargo cleanWindows
powershell
# 查看 Cargo 缓存位置
cargo --versionpowershell
# 清理 Cargo 缓存
cargo clean配置编译优化
macOS/Linux
bash
# 设置发布模式编译
cargo build --releasebash
# 设置并行编译
export RUSTFLAGS="-C target-cpu=native"Windows
powershell
# 设置发布模式编译
cargo build --releasepowershell
# 设置并行编译
$env:RUSTFLAGS = "-C target-cpu=native"✅ 验证安装
完成安装后,验证以下工具是否正常工作:
macOS/Linux
bash
# 检查 Rust
rustc --versionbash
# 检查 Cargo
cargo --versionbash
# 检查 rust-analyzer
rustup component list | grep rust-analyzerbash
# 检查 clippy
rustup component list | grep clippybash
# 检查 rustfmt
rustup component list | grep rustfmtWindows
powershell
# 检查 Rust
rustc --versionpowershell
# 检查 Cargo
cargo --versionpowershell
# 检查 rust-analyzer
rustup component list | Select-String rust-analyzerpowershell
# 检查 clippy
rustup component list | Select-String clippypowershell
# 检查 rustfmt
rustup component list | Select-String rustfmt🎉 下一步
Rust 环境配置完成后,继续配置其他编程语言环境:
遇到问题? 查看 问题排查 页面。