安装和配置Rust开发环境是一个相对简单的过程,主要包括安装Rust编译器、配置文本编辑器和学习使用Cargo。以下是详细的步骤说明:
安装Rust编译器
访问Rust官网:
打开浏览器,访问Rust官网 。安装rustup:
在官网上找到安装指南,并根据你的操作系统选择相应的安装方法。通常,推荐使用rustup工具来安装Rust,它是一个跨平台的安装程序,可以自动为你安装和更新Rust编译器和标准库。自动安装脚本:
在官网上,你可以找到一个自动安装脚本,如下所示(对于Unix-like系统,包括Linux和macOS):curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh在Windows上,你可以下载并运行一个安装程序。
安装过程:
- 对于Unix-like系统,运行上述脚本后,它会询问你是否同意安装条款。输入
yes继续安装。 - 对于Windows,下载安装程序后,运行它并按照向导指示操作。
- 对于Unix-like系统,运行上述脚本后,它会询问你是否同意安装条款。输入
更新和卸载:
使用rustup安装Rust后,你可以通过以下命令来更新或卸载Rust:- 更新:
rustup update - 卸载:
rustup self uninstall
- 更新:
配置文本编辑器
选择文本编辑器:
选择一个你喜欢的文本编辑器或IDE,如Visual Studio Code、Sublime Text、IntelliJ IDEA等。安装Rust插件或扩展:
大多数现代文本编辑器和IDE都支持Rust插件或扩展,提供语法高亮、自动补全、错误检查等功能。在编辑器的插件市场中搜索并安装Rust相关的插件。配置Rust源代码格式:
Rust社区推荐使用rustfmt来格式化代码。你可以将其作为命令行工具使用,或者在编辑器中集成它以自动格式化保存的文件。
rustup 工具链管理深度
rustup 不仅是安装器,更是 Rust 工具链的完整管理器。理解其工作机制对多项目开发至关重要。
工具链切换
# 查看已安装的工具链
rustup toolchain list
# 安装特定版本
rustup toolchain install 1.75.0
rustup toolchain install nightly-2024-01-01
# 为当前目录固定工具链版本
rustup override set 1.75.0
# 项目级工具链锁定(推荐)
# 在项目根目录创建 rust-toolchain.toml
cat > rust-toolchain.toml << 'EOF'
[toolchain]
channel = "1.75.0"
components = ["rustfmt", "clippy", "rust-src"]
targets = ["wasm32-unknown-unknown", "x86_64-pc-windows-gnu"]
EOF
有了这个文件,任何进入该项目目录的开发者都会自动使用指定的 Rust 版本,避免"在我机器上能跑"的问题。
组件管理
| 组件 | 用途 | 安装命令 |
|---|---|---|
rustfmt | 代码格式化 | rustup component add rustfmt |
clippy | 静态分析 linter | rustup component add clippy |
rust-src | 标准库源码(IDE 跳转) | rustup component add rust-src |
rust-docs | 本地文档 | rustup component add rust-docs |
llvm-tools-preview | LLVM 工具链 | rustup component add llvm-tools-preview |
使用Cargo
了解Cargo:
Cargo不仅是Rust的包管理器,还是构建工具。它帮助你管理项目依赖、构建项目和运行测试。创建新项目:
使用以下命令创建一个新的Rust项目:cargo new project_name这将创建一个名为
project_name的新目录,包含Rust项目的模板文件。构建项目:
进入项目目录,使用以下命令构建项目:cargo build这将编译你的代码并生成一个可执行文件。
运行项目:
使用以下命令运行你的项目:cargo run依赖管理:
在Cargo.toml文件中声明依赖,Cargo会自动处理依赖的下载和编译。包和模块:
学习如何将代码组织成包和模块,以及如何在lib.rs或main.rs中声明模块。运行测试:
Rust和Cargo支持编写和运行单元测试。使用以下命令:cargo test构建不同配置:
使用cargo build命令的--release标志来构建优化过的发布版本:cargo build --release学习Cargo文档:
访问Cargo文档 来获取更多关于Cargo的高级用法。
Cargo 高级用法
# 工作空间(多 crate 项目管理)
cargo new --lib my-workspace
cd my-workspace
cat > Cargo.toml << 'EOF'
[workspace]
members = ["crate-a", "crate-b", "crate-c"]
resolver = "2"
EOF
# 条件编译与 feature 标志
cargo build --features "tokio/full,serde"
cargo build --no-default-features # 禁用默认 feature
# 交叉编译(在 macOS 上编译 Linux 可执行文件)
rustup target add x86_64-unknown-linux-musl
cargo build --target x86_64-unknown-linux-musl --release
# 生成并查看文档
cargo doc --open --no-deps
# 分析编译时间(定位慢模块)
cargo build --timings
# 检查代码(不生成二进制,速度快)
cargo check
# 运行特定测试
cargo test my_test_name -- --nocapture
# 发布到 crates.io
cargo login
cargo publish --dry-run # 先模拟发布
cargo publish
VS Code 配置详解
推荐的 .vscode/settings.json:
{
"rust-analyzer.checkOnSave.command": "clippy",
"rust-analyzer.checkOnSave.allTargets": true,
"rust-analyzer.cargo.features": "all",
"rust-analyzer.procMacro.enable": true,
"editor.formatOnSave": true,
"[rust]": {
"editor.defaultFormatter": "rust-lang.rust-analyzer"
},
"rust-analyzer.lens.enable": true,
"rust-analyzer.hover.actions.enable": true
}
必装扩展:
| 扩展 | 功能 |
|---|---|
rust-lang.rust-analyzer | 语言服务器:补全、跳转、重构 |
vadimcn.vscode-lldb | 调试器(LLDB,macOS/Linux) |
ms-vscode.cpptools | 调试器(Windows MSVC) |
serayuzgur.crates | Cargo.toml 依赖版本提示 |
tamasfe.even-better-toml | TOML 格式高亮与校验 |
调试配置
// .vscode/launch.json
{
"version": "0.2.0",
"configurations": [
{
"type": "lldb",
"request": "launch",
"name": "Debug Rust",
"cargo": {
"args": ["build"],
"filter": { "name": "my-app", "kind": "bin" }
},
"args": [],
"cwd": "${workspaceFolder}"
}
]
}
调试技巧:
- 在
Cargo.toml中启用调试信息(默认已启用):[profile.dev] debug = true - 使用
dbg!()宏快速打印变量:dbg!(&my_variable) - Release 模式下调试:
cargo build --profile=release-with-debug
# Cargo.toml
[profile.release-with-debug]
inherits = "release"
debug = true
strip = false
CI/CD 配置示例(GitHub Actions)
name: Rust CI
on: [push, pull_request]
jobs:
test:
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [ubuntu-latest, windows-latest, macos-latest]
rust: [stable, beta]
steps:
- uses: actions/checkout@v4
- name: Install Rust
uses: dtolnay/rust-action@stable
with:
toolchain: ${{ matrix.rust }}
components: rustfmt, clippy
- name: Cache cargo registry
uses: Swatinem/rust-cache@v2
- name: Check formatting
run: cargo fmt -- --check
- name: Run clippy
run: cargo clippy -- -D warnings
- name: Run tests
run: cargo test --all-features
- name: Build release
run: cargo build --release
总结
Rust 开发环境的核心是 rustup(工具链管理)+ cargo(构建与依赖)+ rust-analyzer(IDE 支持)。通过 rust-toolchain.toml 锁定版本、利用 cargo 的 workspace 和 feature 系统管理复杂项目、配合 CI 中的 fmt/clippy/test 三件套保证代码质量,就能建立高效、可复现的 Rust 开发工作流。
建议新开发者在完成环境配置后,先用 cargo new hello 创建一个简单项目,依次尝试 cargo run、cargo test、cargo fmt、cargo clippy,熟悉工具链后再进入实际开发。
继续阅读
探索更多技术文章
浏览归档,发现更多关于系统设计、工具链和工程实践的内容。