安装
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
在本章以及整本书中,我们将展示一些在终端中使用的命令。你应该在终端中输入的行都以
$开头。你不需要输入$字符;它是显示的命令行提示符,用于指示每个命令的开始。不以$开头的行通常显示上一个命令的输出。此外,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
如果你使用的是 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 编译器也很有用,因为一些常用的 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
在 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
要检查你是否正确安装了 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
通过 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
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
本书不对你编写 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
在一些示例中,我们将使用标准库之外的 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.