Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

1. 搭建开发环境

1.1 简介

  在开始学习 Rust 之前,首先需要搭建完整的 Rust 开发环境。本章将介绍 Windows、Linux 和 macOS 系统下 Rust 开发环境的搭建方法,并通过运行第一个 Rust 程序验证环境是否配置成功。

  Rust 官方提供了一套完整的开发工具链,包括编译器、项目管理工具以及开发辅助工具。通过这些工具,开发者可以完成 Rust 程序的编写、编译、运行和发布等完整流程。Rust 开发环境主要包含以下组件:

  • rustc:Rust 编译器,负责将 Rust 源代码编译为可执行程序。
  • cargo:Rust 官方项目管理工具,用于创建项目、管理依赖、编译、测试和发布。
  • rustup:Rust 工具链管理工具,用于安装和切换不同版本的 Rust 编译器,以及最新的稳定版(Stable)、测试版(Beta)和每日构建版(Nightly)。

1.2 各系统下搭建开发环境

1.2.1 Windows 系统搭建开发环境

  Windows 系统下的 Rust 开发环境,可以使用官方提供的 rustup 工具进行安装和管理。Windows 系统下的 Rust 开发环境主要有 MSVC 和 GNU 两种工具链,分别对应 Microsoft Visual C++ 和 GNU 工具链,两者主要区别在于底层编译器、链接器以及与 Windows 系统和第三方库的兼容方式。为了降低开发环境的安装成本,本书选择使用体积更小、安装更加简单的 GNU 工具链作为 Windows 下的 Rust 开发环境。

  • MSVC 工具链:使用 Microsoft Visual C++(MSVC)作为编译和链接工具,能够较好地与 Windows 原生开发环境以及 Visual Studio 生态结合,是 Windows 平台上更常用的选择。
  • GNU 工具链:使用 GCC、MinGW 等 GNU 工具链进行编译,兼容 GNU/Linux 等 Unix-like 平台的开发习惯,适合需要使用 GCC/MinGW 生态或进行跨平台开发的场景。
:: 设置安装目录,可根据需要修改,但建议不要使用中文或特殊字符,避免后续开发过程中出现路径兼容性问题
cmd> mkdir D:\rust\cargo
cmd> mkdir D:\rust\rustc

set CARGO_HOME=D:\rust\cargo
setx CARGO_HOME D:\rust\cargo

cmd> set RUSTUP_HOME=D:\rust\rustc
cmd> setx RUSTUP_HOME D:\rust\rustc

:: 设置环境变量,告诉系统 Cargo 命令在哪个位置
cmd> set PATH=%PATH%;%CARGO_HOME%\bin
cmd> setx PATH "%PATH%;%CARGO_HOME%\bin"

:: RUSTUP 配置国内源
cmd> set RUSTUP_DIST_SERVER=https://mirrors.ustc.edu.cn/rust-static
cmd> set RUSTUP_UPDATE_ROOT=https://mirrors.ustc.edu.cn/rust-static/rustup

cmd> setx RUSTUP_DIST_SERVER https://mirrors.ustc.edu.cn/rust-static
cmd> setx RUSTUP_UPDATE_ROOT https://mirrors.ustc.edu.cn/rust-static/rustup

:: 安装 rustup-init
cmd> curl.exe --proto "=https" --tlsv1.2 -sSf https://win.rustup.rs -o rustup-init.exe

:: 安装 Rust v1.98.0,最新稳定版本可以修改为 stable
cmd> rustup-init.exe --default-toolchain 1.98.0 --default-host x86_64-pc-windows-gnu -y 
cmd> rustup default 1.98.0-x86_64-pc-windows-gnu

rustup-init.exe --default-toolchain stable --default-host x86_64-pc-windows-gnu -y
rustup default stable

:: 安装源码
cmd> rustup component add rust-src
cmd> rustup component add llvm-tools

:: 下载 cargo-binutils,解压到 %CARGO_HOME%\bin
cmd> curl -L -o cargo-binutils.zip https://github.com/rust-embedded/cargo-binutils/releases/download/v0.4.0/x86_64-pc-windows-msvc.zip
tar -xf  --strip-components=1 cargo-binutils.zip tar -C "%CARGO_HOME%\bin"
cmd> rustup component add llvm-tools
cmd> cargo-objdump --version
cargo-objdump 0.4.0

:: 配置 Cargo 国内源,以提高依赖下载速度,避免因网络原因导致下载缓慢或失败
cmd> notepad %CARGO_HOME%\config.toml
[source.crates-io]
replace-with = 'mirror'
[source.mirror]registry = "sparse+https://mirrors.aliyun.com/crates.io-index/"

  由于部分 Rust 第三方库可能包含使用 C/C++ 编写的代码,因此还需要安装 C/C++ 编译器,本书同样选择体积较小的 GNU 工具链。

:: 设置安装目录,可根据需要修改,但建议不要使用中文或特殊字符,避免后续开发过程中出现路径兼容性问题
cmd> set MINGW_HOME=D:\Rust\mingw64
cmd> setx MINGW_HOME D:\Rust\mingw64

:: 下载解压该文件到 MINGW_HOME 指向的安装目录
https://github.com/niXman/mingw-builds-binaries/releases/download/16.2.0-rt_v14-rev1/x86_64-16.2.0-release-posix-seh-ucrt-rt_v14-rev1.7z

:: 设置环境变量,告诉系统 C/C++ 编译器在哪个位置
cmd> set PATH=%PATH%;%MINGW_HOME%\bin
cmd> setx PATH "%PATH%;%MINGW_HOME%\bin"

:: 查看 gcc 编译器版本
cmd> gcc --version
gcc (x86_64-posix-seh-rev1, Built by MinGW-Builds project) 16.2.0
Copyright (C) 2026 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

1.2.2 Linux 系统搭建开发环境

  Linux 系统下的 Rust 开发环境,同样可以使用官方提供的 rustup 工具进行安装和管理。下面以 Ubuntu 26 为例,介绍 Rust 开发环境的安装与配置方法。

# 设置安装目录
shell> sudo mkdir -p /opt/rust/{rustc,cargo} && sudo chown -R `whoami`:`whoami` /opt/rust/
shell> echo 'export RUSTUP_HOME=/opt/rust/rustc' | sudo tee -a /etc/profile
shell> echo 'export CARGO_HOME=/opt/rust/cargo'  | sudo tee -a /etc/profile
shell> echo 'export PATH=$CARGO_HOME/bin:$PATH'  | sudo tee -a /etc/profile
shell> source  /etc/profile

# 配置 RUSTUP 国内镜像,避免网络异常
shell> export RUSTUP_DIST_SERVER=https://mirrors.ustc.edu.cn/rust-static
shell> export RUSTUP_UPDATE_ROOT=https://mirrors.ustc.edu.cn/rust-static/rustup

# 安装 Rust v1.98,如果想使用最新稳定版本可以修改为 stable
shell> curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs  -o rustup-init.sh
shell> bash rustup-init.sh --default-toolchain 1.98.0 --default-host x86_64-unknown-linux-gnu -y
shell> rustup default 1.98.0-x86_64-unknown-linux-gnu

# 设置环境变量
shell> source /opt/rust/cargo/env
shell> rustc --version
rustc 1.98.0 (88d9e12ae 2026-08-18)

# 安装 Rust 源码,以便学习查看
shell> rustup component add rust-src

# 安装 cargo-binutils,内存管理章节需要使用
shell> cargo install cargo-binutils --index sparse+https://mirrors.aliyun.com/crates.io-index/
shell> cargo-objdump --version
cargo-objdump 0.4.0

# 配置 Cargo 国内源,以提高依赖下载速度,避免因网络原因导致下载缓慢或失败
shell> tee /opt/rust/cargo/config <<-'EOF'
[source.crates-io]
replace-with = 'mirror'

[source.mirror]
registry = "sparse+https://mirrors.aliyun.com/crates.io-index/"
EOF
# 安装 gcc 编译器
shell> sudo apt install -y gcc gdb make

# 查看版本号
shell> gcc --version
gcc (Ubuntu 15.2.0-16ubuntu1) 15.2.0
Copyright (C) 2025 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

1.2.3 MacOS 系统搭建开发环境

  • 由于没有 MacOS 电脑,大家可以参考官方安装教程 和 Linux 系统教程搭建开发环境。

2. Hello Rust

  开发环境搭建完成后,就可以创建并运行第一个 Rust 程序,验证开发环境是否配置正确。Rust 官方提供了 Cargo 命令行工具,用于管理 Rust 项目的整个开发流程,包括项目初始化、依赖管理、代码编译、测试以及发布等操作。下面使用 Cargo 命令快速创建一个 Rust 项目,并运行第一个程序。

# 创建项目目录
shell> mkdir app && cd app

# 初始化项目
shell> cargo init
    Creating binary (application) package
note: see more `Cargo.toml` keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

# 编译运行
shell> cargo run
   Compiling app v0.1.0 (D:\Android\Projects\wallpapers\wallpaper\app)
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.73s
     Running `target\debug\app.exe`
Hello, world!            # 成功输出 Hello, world!,说明开发环境搭建成功

  执行 cargo init 后,Cargo 会自动生成 Rust 项目的基础结构, 生成的项目主要包含以下文件和目录:

  • Cargo.toml 是项目配置文件,用于定义项目名称、版本、依赖等信息。

  • src/main.rs 是二进制程序的入口文件,默认生成的代码如下:

// 在 Rust 中,可以使用 // 添加代码注释,用于说明代码的功能和实现逻辑。
// 注释不会参与编译和程序运行,仅用于帮助开发者理解、维护和阅读代码。
fn main() {                      // 主函数,Rust 程序入口,代码从该函数开始执行
    println!("Hello, world!");   // 使用 println! 宏打印输出
}