Rust安装 #

一、安装概述 #

Rust使用rustup工具管理安装,它是一个Rust版本管理器,可以方便地安装、更新和切换Rust版本。

1.1 安装前的准备 #

不同系统需要不同的前置条件:

系统 前置条件
Linux C编译器(gcc或clang)
macOS Xcode命令行工具
Windows Visual Studio Build Tools

二、Linux安装 #

2.1 安装依赖 #

bash
# Debian/Ubuntu
sudo apt update
sudo apt install build-essential

# CentOS/RHEL
sudo yum groupinstall "Development Tools"

# Arch Linux
sudo pacman -S base-devel

2.2 安装rustup #

bash
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh

安装过程中会提示选择安装选项,一般选择默认即可:

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

2.3 配置环境变量 #

bash
source $HOME/.cargo/env

或添加到shell配置文件:

bash
echo 'source $HOME/.cargo/env' >> ~/.bashrc
source ~/.bashrc

三、macOS安装 #

3.1 安装Xcode命令行工具 #

bash
xcode-select --install

3.2 安装rustup #

bash
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh

3.3 配置环境变量 #

bash
source $HOME/.cargo/env

对于zsh用户:

bash
echo 'source $HOME/.cargo/env' >> ~/.zshrc
source ~/.zshrc

四、Windows安装 #

4.1 安装Visual Studio Build Tools #

  1. 下载 Visual Studio Build Tools
  2. 运行安装程序
  3. 选择"使用C++的桌面开发"工作负载
  4. 完成安装

4.2 安装rustup #

  1. 访问 rustup官网
  2. 下载 rustup-init.exe
  3. 运行安装程序
  4. 选择默认安装选项

或使用命令行:

powershell
winget install Rustlang.Rustup

4.3 验证安装 #

打开新的命令提示符或PowerShell:

powershell
rustc --version
cargo --version

五、验证安装 #

5.1 检查版本 #

bash
rustc --version
cargo --version
rustup --version

输出示例:

text
rustc 1.78.0 (9b00956e5 2024-04-29)
cargo 1.78.0 (54d8815d0 2024-03-26)
rustup 1.27.0 (bbb9276d2 2024-03-08)

5.2 运行测试程序 #

创建测试文件:

bash
cat > hello.rs << 'EOF'
fn main() {
    println!("Hello, Rust!");
}
EOF

编译运行:

bash
rustc hello.rs
./hello

输出:

text
Hello, Rust!

六、rustup使用 #

6.1 更新Rust #

bash
rustup update

6.2 查看已安装版本 #

bash
rustup show

6.3 切换版本 #

bash
# 安装特定版本
rustup install 1.77.0

# 切换到特定版本
rustup default 1.77.0

# 切换到最新稳定版
rustup default stable

6.4 安装不同通道 #

bash
# 稳定版(推荐)
rustup default stable

# 测试版
rustup install beta
rustup default beta

# 每夜版(最新特性)
rustup install nightly
rustup default nightly

6.5 按项目设置版本 #

在项目目录创建 rust-toolchain.toml

toml
[toolchain]
channel = "1.78.0"

6.6 查看文档 #

bash
# 打开本地文档
rustup doc

# 打开标准库文档
rustup doc --std

七、配置镜像源(中国大陆) #

7.1 设置环境变量 #

bash
# bash用户
export RUSTUP_DIST_SERVER="https://rsproxy.cn"
export RUSTUP_UPDATE_ROOT="https://rsproxy.cn/rustup"
bash
# 写入配置文件
cat >> ~/.bashrc << 'EOF'
export RUSTUP_DIST_SERVER="https://rsproxy.cn"
export RUSTUP_UPDATE_ROOT="https://rsproxy.cn/rustup"
EOF

7.2 配置Cargo镜像 #

编辑 ~/.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

八、IDE配置 #

8.1 VS Code #

推荐安装扩展:

  • rust-analyzer:官方语言服务器
  • CodeLLDB:调试支持
  • Even Better TOML:TOML文件支持
  • crates:Cargo.toml依赖管理

8.2 IntelliJ IDEA #

安装 Rust 插件。

8.3 其他编辑器 #

  • Vim/Neovim:使用rust-analyzer
  • Emacs:使用rustic模式
  • Helix:内置Rust支持

九、常见问题 #

9.1 链接器错误 #

问题linker 'cc' not found

解决:安装C编译器

bash
# Ubuntu/Debian
sudo apt install build-essential

# macOS
xcode-select --install

9.2 权限问题 #

问题:安装时权限不足

解决:不要使用sudo安装rustup

bash
# 错误做法
sudo curl ... | sh

# 正确做法
curl ... | sh

9.3 环境变量未生效 #

问题rustc: command not found

解决:重新加载环境变量

bash
source $HOME/.cargo/env

9.4 Windows编译错误 #

问题linker 'link.exe' not found

解决:安装Visual Studio Build Tools,选择C++开发工具。

十、卸载Rust #

如需卸载Rust:

bash
rustup self uninstall

十一、总结 #

本章介绍了:

  • 各平台安装Rust的方法
  • rustup版本管理工具使用
  • 镜像源配置
  • IDE开发环境配置
  • 常见问题解决

安装完成后,让我们进入下一章,编写第一个Rust程序。

最后更新:2026-03-27