Rust 版本管理工具 rustup

rustup 是 Rust 官方的版本管理工具,应当作为安装 Rust 的首选。我们在 Linux 和 Mac 系统上通过命令行一键安装 Rust,其实就是下载运行 rustup,然后通过 rustup 安装 Rust。

rustup 项目主页: https://github.com/rust-lang-nursery/rustup.rs

 

1. rustup 的主要用途

  • 管理安装多个官方版本的 Rust 二进制程序。
  • 配置基于目录的 Rust 工具链。
  • 安装和更新来自 Rust 的发布通道: nightly, beta 和 stable。
  • 接收来自发布通道更新的通知。
  • 从官方安装历史版本的 nightly 工具链。
  • 通过指定 stable 版本来安装。
  • 安装额外的 std 用于交叉编译。
  • 安装自定义的工具链。
  • 独立每个安装的 Cargo metadata。
  • 校验下载的 hash 值。
  • 校验签名 (如果 GPG 存在)。
  • 断点续传。
  • 只依赖 bash, curl 和常见 unix 工具。
  • 支持 Linux, OS X, Windows(via MSYS2)。

 

2. Windows 安装 rustup

在 rustup 的 主页 下载并运行 rustup-init.exe,并按照提示选择选项。

Welcome to Rust!

This will download and install the official compiler for the Rust programming
language, and its package manager, Cargo.

It will add the cargo, rustc, rustup and other commands to Cargo's bin
directory, located at:

  C:\Users\Liqueur Librazy\.cargo\bin

This path will then be added to your PATH environment variable by modifying the
HKEY_CURRENT_USER/Environment/PATH registry key.

You can uninstall at any time with rustup self uninstall and these changes will
be reverted.

Current installation options:

   default host triple: x86_64-pc-windows-msvc
     default toolchain: stable
  modify PATH variable: yes

1) Proceed with installation (default)
2) Customize installation
3) Cancel installation

三个选项分别是:

1) 开始安装(默认选项) 2) 自定义安装 3) 取消

其中自定义安装可以更改默认架构与工具链、是否添加 PATH。例如想要选择 nightly 工具链可以进行以下自定义

I'm going to ask you the value of each these installation options.
You may simply press the Enter key to leave unchanged.

Default host triple?


Default toolchain? (stable/beta/nightly)
nightly

Modify PATH variable? (y/n)

设置完毕后,选择 1 以开始安装。

 

3. Linux、Mac 安装 rustup

Linux、Mac 安装 rustup,运行以下命令:

$ curl https://sh.rustup.rs -sSf | sh

这个命令将会编译和安装 rustup, 安装过程中可能会提示你输入 sudo 的密码。 然后, 他会下载和安装 stable 版本的工具链, 当执行 rustc, rustdoc 和 cargo 时, 将会配置他为默认工具链。

安装后工具链会被安装到 $HOME/.cargo/bin 目录。

.cargo/bin 目录会被添加到系统的 $PATH 环境变量,重新登录后即可使用 rustc,cargo 等命令。

 

4. 卸载 Rust rustup

卸载 Rust 及其工具,也就是卸载 rustup,通过以下命令:

$ rustup self uninstall

 

5. rustup 常用命令

rustup -h 显示帮助信息。

rustup default <toolchain> 配置默认工具链。

rustup show 显示当前安装的工具链信息。

rustup update 检查安装更新。

rustup toolchain [SUBCOMMAND] 配置工具链

rustup toolchain install <toolchain> 安装工具链。

rustup toolchain uninstall <toolchain> 卸载工具链。

rustup toolchain link <toolchain-name> "<toolchain-path>" 设置自定义工具链。

rustup override [SUBCOMMAND] 配置一个目录以及其子目录的默认工具链。

  • rustup override set <toolchain> 设置该目录以及其子目录的默认工具链。
  • rustup override unset 取消目录以及其子目录的默认工具链。
  • rustup override list 查看已设置的默认工具链。

rustup target [SUBCOMMAND] 配置工具链的可用目标。

  • rustup target add <target> 安装目标。
  • rustup target remove <target> 卸载目标。
  • rustup target add --toolchain <toolchain> <target> 为特定工具链安装目标。

rustup component 配置 rustup 安装的组件。

  • rustup component add <component> 安装组件。
  • rustup component remove <component> 卸载组件。
  • rustup component list 列出可用组件。

按照编程语言的传统,学习第一门编程语言的第一个程序都是打印 Hello World! 下面我们开始创建 Rust 的 Hello World!程序。开发一个 Rust 程序,需要经过三个步骤:创建文件、编译和运行。