x-i18n: generated_at: “2026-03-01T09:19:55Z” model: gemini-3-flash-preview provider: google-gemini-cli source_hash: 9a6739066ce1229128e99f343fc7e1e7648107dc89456a46683612c50247f118 source_path: ch01-01-installation.md workflow: 16
安装 (Installation)
Installation
第一步是安装 Rust。我们将通过 rustup 下载 Rust,这是一个用于管理 Rust 版本和相关工具的命令行工具。下载过程需要互联网连接。
The first step is to install Rust. We’ll download Rust through rustup, a
command line tool for managing Rust versions and associated tools. You’ll need
an internet connection for the download.
注意:如果由于某种原因你不想使用
rustup,请查看其他 Rust 安装方法页面以获取更多选项。
Note: If you prefer not to use
rustupfor some reason, please see the Other Rust Installation Methods page for more options.
以下步骤将安装最新稳定版本的 Rust 编译器。Rust 的稳定性保证确保了书中所有能编译的示例在更新的 Rust 版本中也能继续编译。不同版本之间的输出可能会略有不同,因为 Rust 经常改进错误消息和警告。换句话说,你使用这些步骤安装的任何更新的稳定版 Rust 都应该能与本书内容配合良好。
The following steps install the latest stable version of the Rust compiler. Rust’s stability guarantees ensure that all the examples in the book that compile will continue to compile with newer Rust versions. The output might differ slightly between versions because Rust often improves error messages and warnings. In other words, any newer, stable version of Rust you install using these steps should work as expected with the content of this book.
命令行表示法 (Command Line Notation)
Command Line Notation
在本章和全书中,我们将展示一些在终端中使用的命令。你应该在终端中输入的行都以
$开头。你不需要输入$字符;它是显示的命令行提示符,用于指示每个命令的开始。不以$开头的行通常显示前一个命令的输出。此外,针对 PowerShell 的示例将使用>而不是$。In this chapter and throughout the book, we’ll show some commands used in the terminal. Lines that you should enter in a terminal all start with
$. You don’t need to type the$character; it’s the command line prompt shown to indicate the start of each command. Lines that don’t start with$typically show the output of the previous command. Additionally, PowerShell-specific examples will use>rather than$.
在 Linux 或 macOS 上安装 rustup (Installing rustup on Linux or macOS)
Installing rustup on Linux or macOS
如果你使用的是 Linux 或 macOS,请打开终端并输入以下命令:
If you’re using Linux or macOS, open a terminal and enter the following command:
$ curl --proto '=https' --tlsv1.2 https://sh.rustup.rs -sSf | sh
该命令下载一个脚本并开始安装 rustup 工具,它会安装最新稳定版本的 Rust。可能会提示你输入密码。如果安装成功,将出现以下行:
The command downloads a script and starts the installation of the rustup
tool, which installs the latest stable version of Rust. You might be prompted
for your password. If the install is successful, the following line will appear:
Rust is installed now. Great!
你还需要一个链接器 (linker),它是 Rust 用于将其编译输出连接成一个文件的程序。你很可能已经有一个了。如果你遇到链接器错误,你应该安装一个 C 编译器 (C compiler),它通常会包含一个链接器。C 编译器也很有用,因为一些常用的 Rust 包依赖于 C 代码,因此需要 C 编译器。
You will also need a linker, which is a program that Rust uses to join its compiled outputs into one file. It is likely you already have one. If you get linker errors, you should install a C compiler, which will typically include a linker. A C compiler is also useful because some common Rust packages depend on C code and will need a C compiler.
在 macOS 上,你可以通过运行以下命令获取 C 编译器:
On macOS, you can get a C compiler by running:
$ xcode-select --install
Linux 用户通常应根据其发行版的文档安装 GCC 或 Clang。例如,如果你使用 Ubuntu,可以安装 build-essential 包。
Linux users should generally install GCC or Clang, according to their
distribution’s documentation. For example, if you use Ubuntu, you can install
the build-essential package.
在 Windows 上安装 rustup (Installing rustup on Windows)
Installing rustup on Windows
在 Windows 上,请访问 https://www.rust-lang.org/tools/install 并按照安装 Rust 的说明进行操作。在安装过程中的某个时刻,系统会提示你安装 Visual Studio。这提供了链接器和编译程序所需的原生库。如果你在这一步需要更多帮助,请参阅 https://rust-lang.github.io/rustup/installation/windows-msvc.html。
On Windows, go to https://www.rust-lang.org/tools/install and follow the instructions for installing Rust. At some point in the installation, you’ll be prompted to install Visual Studio. This provides a linker and the native libraries needed to compile programs. If you need more help with this step, see https://rust-lang.github.io/rustup/installation/windows-msvc.html.
本书的其余部分使用的命令在 cmd.exe 和 PowerShell 中均适用。如果有特定的差异,我们会解释使用哪一个。
The rest of this book uses commands that work in both cmd.exe and PowerShell. If there are specific differences, we’ll explain which to use.
故障排除 (Troubleshooting)
Troubleshooting
要检查你是否正确安装了 Rust,请打开一个 Shell 并输入这一行:
To check whether you have Rust installed correctly, open a shell and enter this line:
$ rustc --version
你应该会看到最新发布的稳定版本的版本号、提交哈希和提交日期,格式如下:
You should see the version number, commit hash, and commit date for the latest stable version that has been released, in the following format:
rustc x.y.z (abcabcabc yyyy-mm-dd)
如果你看到了这些信息,说明你已经成功安装了 Rust!如果你没有看到这些信息,请按照以下步骤检查 Rust 是否在你的 %PATH% 系统变量中。
If you see this information, you have installed Rust successfully! If you don’t
see this information, check that Rust is in your %PATH% system variable as
follows.
在 Windows CMD 中,使用:
In Windows CMD, use:
> echo %PATH%
在 PowerShell 中,使用:
In PowerShell, use:
> echo $env:Path
在 Linux 和 macOS 中,使用:
In Linux and macOS, use:
$ echo $PATH
如果一切都正确但 Rust 仍然无法工作,有很多地方可以寻求帮助。在 社区页面 上查找如何与其他 Rustaceans(我们自称的一个有趣的昵称)取得联系。
If that’s all correct and Rust still isn’t working, there are a number of places you can get help. Find out how to get in touch with other Rustaceans (a silly nickname we call ourselves) on the community page.
更新与卸载 (Updating and Uninstalling)
Updating and Uninstalling
一旦通过 rustup 安装了 Rust,更新到新发布的版本就很简单。在 Shell 中运行以下更新脚本:
Once Rust is installed via rustup, updating to a newly released version is
easy. From your shell, run the following update script:
$ rustup update
要卸载 Rust 和 rustup,请在 Shell 中运行以下卸载脚本:
To uninstall Rust and rustup, run the following uninstall script from your
shell:
$ rustup self uninstall
阅读本地文档 (Reading the Local Documentation)
Reading the Local Documentation
Rust 的安装还包括一份文档的本地副本,以便你可以离线阅读。运行 rustup doc 即可在浏览器中打开本地文档。
The installation of Rust also includes a local copy of the documentation so
that you can read it offline. Run rustup doc to open the local documentation
in your browser.
每当标准库提供一个类型或函数,而你不确定它的作用或如何使用它时,请查阅应用编程接口 (API) 文档来查找答案!
Any time a type or function is provided by the standard library and you’re not sure what it does or how to use it, use the application programming interface (API) documentation to find out!
使用文本编辑器和 IDE (Using Text Editors and IDEs)
Using Text Editors and IDEs
本书不假设你使用什么工具来编写 Rust 代码。几乎任何文本编辑器都可以胜任!但是,许多文本编辑器和集成开发环境 (IDE) 都有对 Rust 的内置支持。你始终可以在 Rust 网站的 工具页面 上找到许多编辑器和 IDE 的相当新的列表。
This book makes no assumptions about what tools you use to author Rust code. Just about any text editor will get the job done! However, many text editors and integrated development environments (IDEs) have built-in support for Rust. You can always find a fairly current list of many editors and IDEs on the tools page on the Rust website.
离线使用本书 (Working Offline with This Book)
Working Offline with This Book
在几个示例中,我们将使用标准库之外的 Rust 包。要完成这些示例,你既需要互联网连接,也需要提前下载这些依赖项。要提前下载这些依赖项,可以运行以下命令。(我们稍后会详细解释 cargo 是什么以及每个命令的作用。)
In several examples, we will use Rust packages beyond the standard library. To
work through those examples, you will either need to have an internet connection
or to have downloaded those dependencies ahead of time. To download the
dependencies ahead of time, you can run the following commands. (We’ll explain
what cargo is and what each of these commands does in detail later.)
$ cargo new get-dependencies
$ cd get-dependencies
$ cargo add rand@0.8.5 trpl@0.2.0
这将缓存这些包的下载内容,因此你稍后无需下载它们。一旦运行了此命令,就不需要保留 get-dependencies 文件夹了。如果你运行了此命令,则可以在本书其余部分的所有 cargo 命令中使用 --offline 标志,以使用这些缓存版本而不是尝试使用网络。
This will cache the downloads for these packages so you will not need to
download them later. Once you have run this command, you do not need to keep the
get-dependencies folder. If you have run this command, you can use the
--offline flag with all cargo commands in the rest of the book to use these
cached versions instead of attempting to use the network.