Skip to content

Rust 环境配置

本章节将指导你配置 Rust 开发环境,包括 Rust 安装、包管理、开发工具配置等。

📋 配置清单

  • Rust 安装
  • 镜像配置
  • 开发工具安装
  • 项目配置
  • 验证安装

1. 安装 Rust

使用 rustup 安装

macOS/Linux

bash
# 安装 Rust
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
bash
# 重新加载配置
source ~/.cargo/env

Windows

powershell
# 安装 Rust
# 访问: https://rustup.rs/
# 下载并运行 rustup-init.exe

验证安装

macOS/Linux

bash
# 检查 Rust 版本
rustc --version
bash
# 检查 Cargo 版本
cargo --version

Windows

powershell
# 检查 Rust 版本
rustc --version
powershell
# 检查 Cargo 版本
cargo --version

2. 配置 Rust 镜像

配置 Cargo 镜像

macOS/Linux

bash
# 创建 Cargo 配置目录
mkdir -p ~/.cargo
bash
# 配置镜像
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/config

Windows

powershell
# 创建 Cargo 配置目录
New-Item -Path ~/.cargo -ItemType Directory -Force
powershell
# 创建配置文件
New-Item -Path ~/.cargo/config -ItemType File -Force
powershell
# 配置镜像
@"
[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 UTF8

3. 安装开发工具

安装 Rust 组件

macOS/Linux

bash
# 安装 rust-analyzer (语言服务器)
rustup component add rust-analyzer
bash
# 安装 clippy (代码检查工具)
rustup component add clippy
bash
# 安装 rustfmt (代码格式化工具)
rustup component add rustfmt
bash
# 安装 cargo-audit (安全审计工具)
cargo install cargo-audit

Windows

powershell
# 安装 rust-analyzer (语言服务器)
rustup component add rust-analyzer
powershell
# 安装 clippy (代码检查工具)
rustup component add clippy
powershell
# 安装 rustfmt (代码格式化工具)
rustup component add rustfmt
powershell
# 安装 cargo-audit (安全审计工具)
cargo install cargo-audit

安装常用工具

macOS/Linux

bash
# 安装 cargo-watch (文件监控)
cargo install cargo-watch
bash
# 安装 cargo-expand (宏展开)
cargo install cargo-expand
bash
# 安装 cargo-tree (依赖树)
cargo install cargo-tree

Windows

powershell
# 安装 cargo-watch (文件监控)
cargo install cargo-watch
powershell
# 安装 cargo-expand (宏展开)
cargo install cargo-expand
powershell
# 安装 cargo-tree (依赖树)
cargo install cargo-tree

4. 项目配置

创建新项目

macOS/Linux

bash
# 创建二进制项目
cargo new myproject
cd myproject
bash
# 创建库项目
cargo new mylib --lib
cd mylib

Windows

powershell
# 创建二进制项目
cargo new myproject
cd myproject
powershell
# 创建库项目
cargo new mylib --lib
cd mylib

项目操作

macOS/Linux

bash
# 构建项目
cargo build
bash
# 运行项目
cargo run
bash
# 运行测试
cargo test
bash
# 检查代码
cargo check
bash
# 格式化代码
cargo fmt
bash
# 代码检查
cargo clippy

Windows

powershell
# 构建项目
cargo build
powershell
# 运行项目
cargo run
powershell
# 运行测试
cargo test
powershell
# 检查代码
cargo check
powershell
# 格式化代码
cargo fmt
powershell
# 代码检查
cargo clippy

5. 配置文件

创建 .gitignore

macOS/Linux

bash
# 创建 .gitignore 文件
touch .gitignore
bash
# 添加 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" > .gitignore

Windows

powershell
# 创建 .gitignore 文件
New-Item -Path .gitignore -ItemType File
powershell
# 添加 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 UTF8

6. 调试配置

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 --version
bash
# 清理 Cargo 缓存
cargo clean

Windows

powershell
# 查看 Cargo 缓存位置
cargo --version
powershell
# 清理 Cargo 缓存
cargo clean

配置编译优化

macOS/Linux

bash
# 设置发布模式编译
cargo build --release
bash
# 设置并行编译
export RUSTFLAGS="-C target-cpu=native"

Windows

powershell
# 设置发布模式编译
cargo build --release
powershell
# 设置并行编译
$env:RUSTFLAGS = "-C target-cpu=native"

✅ 验证安装

完成安装后,验证以下工具是否正常工作:

macOS/Linux

bash
# 检查 Rust
rustc --version
bash
# 检查 Cargo
cargo --version
bash
# 检查 rust-analyzer
rustup component list | grep rust-analyzer
bash
# 检查 clippy
rustup component list | grep clippy
bash
# 检查 rustfmt
rustup component list | grep rustfmt

Windows

powershell
# 检查 Rust
rustc --version
powershell
# 检查 Cargo
cargo --version
powershell
# 检查 rust-analyzer
rustup component list | Select-String rust-analyzer
powershell
# 检查 clippy
rustup component list | Select-String clippy
powershell
# 检查 rustfmt
rustup component list | Select-String rustfmt

🎉 下一步

Rust 环境配置完成后,继续配置其他编程语言环境:


遇到问题? 查看 问题排查 页面。

Released under the MIT License.