Cargo 安装与配置 #

安装概览 #

Cargo 通常随 Rust 一起安装,通过 rustup 工具可以轻松管理 Rust 和 Cargo 的版本。本节将详细介绍各平台的安装方法和配置技巧。

安装方式 #

方式一:rustup 安装(推荐) #

rustup 是 Rust 官方的版本管理工具,推荐所有开发者使用。

Linux / macOS #

bash
# 下载并运行安装脚本
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh

# 或者使用 wget
wget -qO- https://sh.rustup.rs | sh

安装过程中会提示选择安装选项:

text
1) Proceed with installation (default)
2) Customize installation
3) Cancel installation

选择 1 使用默认配置即可。

Windows #

方式一:下载安装程序

  1. 访问 https://rustup.rs/
  2. 下载 rustup-init.exe
  3. 运行安装程序,按照提示完成安装

方式二:使用包管理器

powershell
# 使用 scoop
scoop install rustup

# 使用 chocolatey
choco install rustup.install

验证安装 #

bash
# 检查 Rust 版本
rustc --version

# 检查 Cargo 版本
cargo --version

# 查看 rustup 信息
rustup show

方式二:包管理器安装 #

Linux 发行版 #

bash
# Ubuntu / Debian
sudo apt update
sudo apt install rustc cargo

# Fedora
sudo dnf install rust cargo

# Arch Linux
sudo pacman -S rust cargo

# Alpine Linux
apk add rust cargo

macOS #

bash
# 使用 Homebrew
brew install rust

⚠️ 注意:包管理器安装的版本可能不是最新,推荐使用 rustup。

方式三:离线安装 #

适用于无网络环境:

  1. https://rust-lang.org/downloads.html 下载离线安装包
  2. 解压并运行安装脚本
bash
# Linux / macOS
tar -xzf rust-1.xx.x-x86_64-unknown-linux-gnu.tar.gz
cd rust-1.xx.x-x86_64-unknown-linux-gnu
./install.sh

# Windows
# 运行下载的 .msi 安装包

环境变量配置 #

核心环境变量 #

安装完成后,以下环境变量会被自动配置:

变量 描述 默认值
PATH 包含 Cargo 和 Rust 二进制目录 ~/.cargo/bin
RUSTUP_HOME rustup 安装目录 ~/.rustup
CARGO_HOME Cargo 主目录 ~/.cargo

手动配置环境变量 #

如果环境变量未正确设置,可以手动添加:

Linux / macOS #

编辑 ~/.bashrc~/.zshrc

bash
# 添加 Cargo 到 PATH
export PATH="$HOME/.cargo/bin:$PATH"

# 设置 Cargo 主目录(可选)
export CARGO_HOME="$HOME/.cargo"

# 设置 rustup 主目录(可选)
export RUSTUP_HOME="$HOME/.rustup"

应用配置:

bash
source ~/.bashrc
# 或
source ~/.zshrc

Windows #

powershell
# 通过系统设置添加环境变量
# 或使用 PowerShell

[Environment]::SetEnvironmentVariable("Path", $env:Path + ";$env:USERPROFILE\.cargo\bin", "User")

自定义安装位置 #

bash
# 安装前设置
export RUSTUP_HOME=/custom/path/rustup
export CARGO_HOME=/custom/path/cargo

# 然后运行安装脚本
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh

rustup 常用命令 #

版本管理 #

bash
# 查看已安装版本
rustup show

# 安装特定版本
rustup install 1.75.0
rustup install stable
rustup install beta
rustup install nightly

# 设置默认版本
rustup default stable
rustup default 1.75.0

# 切换版本
rustup override set nightly    # 当前目录使用 nightly
rustup override unset          # 取消覆盖

# 更新 Rust
rustup update                  # 更新所有版本
rustup update stable           # 更新 stable 版本

工具链管理 #

bash
# 查看已安装的工具链
rustup toolchain list

# 安装工具链
rustup toolchain install stable
rustup toolchain install nightly-2024-01-01

# 卸载工具链
rustup toolchain uninstall 1.70.0

组件管理 #

bash
# 查看已安装组件
rustup component list
rustup component list --installed

# 安装组件
rustup component add clippy      # 代码检查工具
rustup component add rustfmt     # 代码格式化工具
rustup component add rust-src    # Rust 源代码
rustup component add rust-analyzer  # LSP 服务器

# 卸载组件
rustup component remove rust-docs

目标平台管理 #

bash
# 查看已安装目标
rustup target list
rustup target list --installed

# 添加编译目标
rustup target add x86_64-pc-windows-gnu
rustup target add aarch64-unknown-linux-gnu
rustup target add wasm32-unknown-unknown

# 移除编译目标
rustup target remove x86_64-pc-windows-gnu

Cargo 配置 #

配置文件位置 #

Cargo 的配置文件可以放在以下位置:

位置 优先级 描述
.cargo/config.toml 最高 项目级配置
~/.cargo/config.toml 用户级配置
$CARGO_HOME/config.toml Cargo 主目录配置

基本配置示例 #

创建 .cargo/config.toml 文件:

toml
# 构建配置
[build]
jobs = 4                    # 并行编译任务数
target-dir = "target"       # 编译输出目录
rustflags = ["-C", "target-cpu=native"]  # 编译器标志

# 网络配置
[net]
retry = 3                   # 网络重试次数
offline = false             # 离线模式

# 注册表配置
[registry]
default = "crates-io"       # 默认注册表

编译器配置 #

toml
# 针对 debug 模式
[profile.dev]
opt-level = 0               # 优化级别
debug = true                # 包含调试信息
split-debuginfo = "packed"

# 针对 release 模式
[profile.release]
opt-level = 3               # 最高优化
lto = true                  # 链接时优化
codegen-units = 1           # 代码生成单元
strip = true                # 移除符号信息

环境变量配置 #

toml
[env]
RUST_LOG = "debug"
RUST_BACKTRACE = "1"
MY_VARIABLE = "value"

镜像源配置 #

由于网络原因,国内访问 crates.io 可能较慢,可以配置国内镜像源。

字节跳动镜像(推荐) #

创建或编辑 ~/.cargo/config.toml

toml
[source.crates-io]
replace-with = 'rsproxy-sparse'

[source.rsproxy]
registry = "https://rsproxy.cn/crates.io-index"

[source.rsproxy-sparse]
registry = "sparse+https://rsproxy.cn/index/"

[registries.rsproxy]
index = "https://rsproxy.cn/crates.io-index"

[net]
git-fetch-with-cli = true

清华大学镜像 #

toml
[source.crates-io]
replace-with = 'tuna'

[source.tuna]
registry = "https://mirrors.tuna.tsinghua.edu.cn/git/crates.io-index.git"

中科大镜像 #

toml
[source.crates-io]
replace-with = 'ustc'

[source.ustc]
registry = "https://mirrors.ustc.edu.cn/crates.io-index"

Rust CC 镜像 #

toml
[source.crates-io]
replace-with = 'rustcc'

[source.rustcc]
registry = "https://code.aliyun.com/rustcc/crates.io-index.git"

Sparse 协议(推荐) #

Cargo 1.68+ 支持 sparse 协议,速度更快:

toml
[source.crates-io]
replace-with = "rsproxy-sparse"

[source.rsproxy-sparse]
registry = "sparse+https://rsproxy.cn/index/"

或者直接使用官方 sparse 协议:

bash
# 设置环境变量
export CARGO_REGISTRIES_CRATES_IO_PROTOCOL=sparse

# 或在配置文件中
[registry]
protocol = "sparse"

Git 配置 #

使用系统 Git #

toml
[net]
git-fetch-with-cli = true

Git 代理配置 #

bash
# 配置 Git 代理
git config --global http.proxy http://127.0.0.1:7890
git config --global https.proxy http://127.0.0.1:7890

# 取消代理
git config --global --unset http.proxy
git config --global --unset https.proxy

SSH 密钥配置 #

bash
# 生成 SSH 密钥
ssh-keygen -t ed25519 -C "your_email@example.com"

# 添加到 ssh-agent
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_ed25519

IDE 配置 #

VS Code #

安装推荐扩展:

json
{
  "recommendations": [
    "rust-lang.rust-analyzer",
    "tamasfe.even-better-toml",
    "serayuzgur.crates",
    "vadimcn.vscode-lldb"
  ]
}

配置 settings.json

json
{
  "rust-analyzer.checkOnSave.command": "clippy",
  "rust-analyzer.cargo.features": "all",
  "[rust]": {
    "editor.defaultFormatter": "rust-lang.rust-analyzer",
    "editor.formatOnSave": true
  }
}

IntelliJ IDEA / CLion #

  1. 安装 Rust 插件
  2. 配置工具链:Settings → Languages & Frameworks → Rust
  3. 设置 Cargo 路径和标准库路径

Vim / Neovim #

使用 coc.nvimnvim-lspconfig 配置 rust-analyzer:

lua
-- nvim-lspconfig
require'lspconfig'.rust_analyzer.setup{
  settings = {
    ['rust-analyzer'] = {
      checkOnSave = {
        command = "clippy"
      }
    }
  }
}

常见问题 #

1. 权限问题 #

bash
# 错误:Permission denied
# 解决:修改目录权限
sudo chown -R $(whoami) ~/.cargo ~/.rustup

2. 网络超时 #

bash
# 错误:Connection timed out
# 解决:配置镜像源或代理

# 使用代理
export HTTP_PROXY=http://127.0.0.1:7890
export HTTPS_PROXY=http://127.0.0.1:7890

3. 版本不兼容 #

bash
# 错误:rustc version mismatch
# 解决:更新 Rust 版本
rustup update

# 或安装项目需要的特定版本
rustup install 1.70.0
rustup override set 1.70.0

4. 清理缓存 #

bash
# 清理 Cargo 缓存
cargo cache --autoclean

# 或手动删除
rm -rf ~/.cargo/registry/cache
rm -rf ~/.cargo/registry/index
rm -rf ~/.cargo/registry/src

5. 重装 Cargo #

bash
# 卸载
rustup self uninstall

# 重新安装
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh

验证安装 #

完成安装后,运行以下命令验证:

bash
# 检查版本
cargo --version
rustc --version
rustup --version

# 创建测试项目
cargo new hello_cargo
cd hello_cargo
cargo run

# 预期输出
# Hello, world!

下一步 #

环境配置完成后,继续学习 基本命令 开始使用 Cargo 管理项目!

最后更新:2026-03-28