Rust学习 - 01 环境搭建

Rust 环境搭建笔记

1. 安装 rustup

rustup 是 Rust 的工具链管理器(toolchain manager),不是编译器本身,而是管理 rustc/cargo 版本的工具。

安装步骤

  1. 访问 https://www.rust-lang.org/tools/install,下载 rustup-init.exe
  2. 运行安装程序,确认默认选项:
    • default host triple: x86_64-pc-windows-msvc — 使用 MSVC 链接器
    • default toolchain: stable
    • modify PATH variable: yes
  3. 选择 1) Proceed with standard installation,等待安装完成
  4. 关闭当前终端,重新打开新终端(PATH 需要刷新)

安装验证

rustc --version   # rustc 1.94.0 (4a4ef493e 2026-03-02)
cargo --version   # cargo 1.94.0 (85eff7c80 2026-01-15)
rustup show       # 查看已安装的工具链和当前激活的工具链

2. 安装 nightly 工具链

rustup install nightly

日常学习用 stable,后续写内核驱动(#![no_std])需要 nightly,因为部分底层 feature 还是 unstable。

3. 安装常用组件

rustup component add rustfmt clippy
  • rustfmt — 代码格式化工具(类似 clang-format)
  • clippy — 静态分析/lint 工具(类似 MSVC 的 /analyze 或 PREfast)

4. 关键概念

rustup vs rustc vs cargo

工具 作用 类比 C/C++
rustup 工具链管理器,安装/切换/更新 rustc 和 cargo Visual Studio Installer
rustc Rust 编译器 cl.exe
cargo 包管理器 + 构建系统 vcpkg + MSBuild 的组合

Release Channel(发布通道)

Channel 说明 更新频率 类比 MSVC
stable 稳定版,保证向后兼容 每 6 周 /std:c++20
beta 下一个 stable 的候选版 每 6 周
nightly 每日构建,含实验性功能 每天 /std:c++latest

通过 #![feature(xxx)] 启用 nightly 专属特性。

为什么 Windows 上需要 MSVC

Rust 编译器把 .rs 编译成 COFF 格式的 .obj,但链接这一步调用的是 MSVC 的 link.exe。和 WDK 编译内核驱动用 link.exe 链接 .sys 是同一个流程。

Windows 上的两个 target:

  • x86_64-pc-windows-msvc — 用 MSVC link.exe(内核驱动必须用这个
  • x86_64-pc-windows-gnu — 用 MinGW/GCC 链接器

5. 常用命令速查

rustup show                    # 查看当前工具链
rustup update                  # 更新所有已安装的工具链
rustup default stable          # 切换默认工具链为 stable
rustup default nightly         # 切换默认工具链为 nightly
rustup doc                     # 打开本地离线文档(Rust 版 MSDN)
posted @ 2026-03-12 17:15  BitWarden  阅读(1)  评论(0)    收藏  举报