安装Rust

Rust 是一种系统级编程语言,旨在提供高性能和内存安全,同时避免常见的编程错误。
由 Mozilla Research 推出,Rust 自推出以来因其独特的设计理念和强大的功能而在开发者社区中迅速获得了广泛的关注和采用。

curl --proto ‘=https’ --tlsv1.2 -sSf https://sh.rustup.rs | sh info: downloading installer

Welcome to Rust!

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

Rustup metadata and toolchains will be installed into the Rustup home
directory, located at:

/home/jjmczd/.rustup

This can be modified with the RUSTUP_HOME environment variable.

The Cargo home directory is located at:

/home/jjmczd/.cargo

This can be modified with the CARGO_HOME environment variable.

The cargo, rustc, rustup and other commands will be added to Cargo’s
bin directory, located at:

/home/jjmczd/.cargo/bin

This path will then be added to your PATH environment variable by
modifying the profile files located at:

/home/jjmczd/.profile /home/jjmczd/.bashrc

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

Current installation options:

default host triple: x86_64-unknown-linux-gnu
default toolchain: stable (default)
profile: default modify PATH variable: yes

  1. Proceed with standard installation (default - just press enter) 2)
    Customize installation 3) Cancel installation

命令解释

curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh

分解说明:

  1. curl:这是一个用于在命令行下传输数据的工具,支持多种协议(如 HTTP、HTTPS、FTP 等)。

  2. --proto '=https':指定只允许使用 HTTPS 协议进行传输,确保数据传输的安全性。

  3. --tlsv1.2:强制 curl 使用 TLS 1.2 协议,这是一种安全的传输层协议。

  4. -s:静默模式(silent),在执行过程中不会显示进度条或错误信息。

  5. -Sf

    • -S:当使用 -s(静默模式)时,-S 可以让 curl 在发生错误时仍然显示错误信息。
    • -f:如果服务器返回一个错误状态码(如 404),curl 会失败并返回一个错误,而不是输出错误页面的内容。
  6. https://sh.rustup.rs:这是 Rust 官方提供的安装脚本的 URL。

  7. | sh:管道符号(|)将前一个命令(curl)的输出传递给后一个命令(sh)。也就是说,下载的安装脚本将直接由 sh(shell)执行。

整体作用:
这个命令通过安全的 HTTPS 连接下载 Rust 的安装脚本,并立即在您的终端中执行该脚本,以便安装 Rust 编程语言及其工具链。

输出解释

info: downloading installer

Welcome to Rust!

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

Rustup metadata and toolchains will be installed into the Rustup
home directory, located at:

  /home/jjmczd/.rustup

This can be modified with the RUSTUP_HOME environment variable.

The Cargo home directory is located at:

  /home/jjmczd/.cargo

This can be modified with the CARGO_HOME environment variable.

The cargo, rustc, rustup and other commands will be added to
Cargo's bin directory, located at:

  /home/jjmczd/.cargo/bin

This path will then be added to your PATH environment variable by
modifying the profile files located at:

  /home/jjmczd/.profile
  /home/jjmczd/.bashrc

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

Current installation options:

   default host triple: x86_64-unknown-linux-gnu
     default toolchain: stable (default)
               profile: default
  modify PATH variable: yes

1) Proceed with standard installation (default - just press enter)
2) Customize installation
3) Cancel installation

逐行解释:

  1. info: downloading installer

    • 解释:安装程序正在下载过程中。
  2. Welcome to Rust!

    • 解释:欢迎使用 Rust!
  3. 接下来的几行

    • 解释:这些行说明了安装过程将会下载和安装 Rust 官方编译器(rustc)以及其包管理器(Cargo)。
  4. Rustup metadata and toolchains will be installed into the Rustup home directory, located at:

    • 解释:Rustup 的元数据和工具链将被安装到指定的 Rustup 主目录中,默认路径为 /home/jjmczd/.rustup。您可以通过设置 RUSTUP_HOME 环境变量来修改此路径。
  5. The Cargo home directory is located at:

    • 解释:Cargo 的主目录位于 /home/jjmczd/.cargo。同样,您可以通过设置 CARGO_HOME 环境变量来修改此路径。
  6. The cargo, rustc, rustup and other commands will be added to Cargo's bin directory, located at:

    • 解释cargorustcrustup 以及其他相关命令将被添加到 Cargo 的 bin 目录中,即 /home/jjmczd/.cargo/bin
  7. This path will then be added to your PATH environment variable by modifying the profile files located at:

    • 解释:安装程序会将上述 bin 目录路径添加到您的 PATH 环境变量中,这通过修改您的 shell 配置文件(如 /home/jjmczd/.profile/home/jjmczd/.bashrc)来实现。这样,您可以在任何终端会话中直接运行 Rust 的命令。
  8. You can uninstall at any time with rustup self uninstall and these changes will be reverted.

    • 解释:如果您在任何时候想要卸载 Rust,可以运行 rustup self uninstall 命令,这将撤销所有安装的更改。
  9. Current installation options:

    • 解释:当前的安装选项如下:

    • default host triple: x86_64-unknown-linux-gnu

      • 解释:默认的主机三元组(host triple)是 x86_64-unknown-linux-gnu,表示安装的是适用于 64 位 Linux 系统的 Rust 工具链。
    • default toolchain: stable (default)

      • 解释:默认的工具链是 stable 版本,这是 Rust 的稳定版本,适合大多数用户和生产环境使用。
    • profile: default

      • 解释:使用的是默认的安装配置文件,包含基本的组件和设置。
    • modify PATH variable: yes

      • 解释:安装程序将修改您的 PATH 环境变量,以便您可以在终端中直接使用 Rust 的命令。
  10. 安装选项菜单:

    1) Proceed with standard installation (default - just press enter)
    2) Customize installation
    3) Cancel installation
    
    • 1) Proceed with standard installation (default - just press enter)

      • 解释:继续标准安装(默认选项)。如果您按回车键,将使用上述默认设置进行安装。
    • 2) Customize installation

      • 解释:自定义安装。选择此选项可以让您自定义安装路径、选择不同的工具链版本或调整其他安装选项。
    • 3) Cancel installation

      • 解释:取消安装。选择此选项将终止 Rust 的安装过程。

接下来的步骤

  1. 选择安装选项:

    • 标准安装:如果您不需要自定义安装,直接按回车键继续。这将使用默认设置进行安装。
    • 自定义安装:如果您需要更改安装路径或选择特定的工具链版本,可以输入 2 并按照提示进行操作。
    • 取消安装:如果您暂时不想安装 Rust,可以输入 3 取消。
  2. 完成安装:

    • 安装完成后,确保重新启动终端或重新加载 shell 配置文件,以便新的 PATH 设置生效。

    • 您可以通过运行以下命令来验证 Rust 是否安装成功:

      rustc --version
      cargo --version
      

      这两个命令应分别返回 Rust 编译器和 Cargo 的版本信息。

  3. 更新 Rust(可选):

    • 如果您已经安装过 Rust,可以通过以下命令更新到最新版本:

      rustup update
      

常见问题

  • PATH 环境变量未更新:
    如果安装后运行 rustc --version 提示找不到命令,可能是因为 PATH 环境变量未正确更新。您可以手动添加 Cargo 的 bin 目录到 PATH 中,例如:

    export PATH="$HOME/.cargo/bin:$PATH"
    

    将上述行添加到您的 ~/.bashrc~/.profile 文件中,然后重新加载配置:

    source ~/.bashrc
    
  • 卸载 Rust:
    如果需要卸载 Rust,可以运行以下命令:

    rustup self uninstall
    

    这将删除 Rustup 及其管理的所有工具链,并恢复 PATH 环境变量的更改。

info: profile set to ‘default’ info: default host triple is x86_64-unknown-linux-gnu info: syncing channel updates for
‘stable-x86_64-unknown-linux-gnu’
804.2 KiB / 804.2 KiB (100 %) 86.3 KiB/s in 10s ETA: 0s info: latest update on 2024-09-05, rust version 1.81.0 (eeb90cda1 2024-09-04) info:
downloading component ‘cargo’
8.3 MiB / 8.3 MiB (100 %) 243.2 KiB/s in 43s ETA: 0s info: downloading component ‘clippy’
2.6 MiB / 2.6 MiB (100 %) 488.2 KiB/s in 8s ETA: 0s info: downloading component ‘rust-docs’
15.9 MiB / 15.9 MiB (100 %) 962.2 KiB/s in 1m 13s ETA: 0s info: downloading component ‘rust-std’
26.8 MiB / 26.8 MiB (100 %) 298.8 KiB/s in 1m 37s ETA: 0s info: downloading component ‘rustc’
66.9 MiB / 66.9 MiB (100 %) 198.4 KiB/s in 9m 28s ETA: 0s info: downloading component ‘rustfmt’
2.4 MiB / 2.4 MiB (100 %) 438.4 KiB/s in 8s ETA: 0s info: installing component ‘cargo’ info: installing component ‘clippy’ info:
installing component ‘rust-docs’
15.9 MiB / 15.9 MiB (100 %) 5.8 MiB/s in 2s ETA: 0s info: installing component ‘rust-std’
26.8 MiB / 26.8 MiB (100 %) 14.7 MiB/s in 2s ETA: 0s info: installing component ‘rustc’
66.9 MiB / 66.9 MiB (100 %) 15.7 MiB/s in 4s ETA: 0s info: installing component ‘rustfmt’ info: default toolchain set to
‘stable-x86_64-unknown-linux-gnu’

stable-x86_64-unknown-linux-gnu installed - rustc 1.81.0 (eeb90cda1
2024-09-04)

Rust is installed now. Great!

To get started you may need to restart your current shell. This would
reload your PATH environment variable to include Cargo’s bin directory
($HOME/.cargo/bin).

To configure your current shell, you need to source the corresponding
env file under $HOME/.cargo.

This is usually done by running one of the following (note the leading
DOT): . “KaTeX parse error: Expected 'EOF', got '#' at position 29: …nv" #̲ For sh/bash/zs…HOME/.cargo/env.fish” # For fish
这个输出是关于在终端中安装 Rust 的详细日志。每一行解释如下:

  1. info: profile set to ‘default’
    Rust 安装时选择了默认的配置(profile),这意味着 Rust 会根据默认的设置安装其组件。这通常包括常用的工具,比如编译器和包管理器。

  2. info: default host triple is x86_64-unknown-linux-gnu
    这是 Rust 编译器的目标平台,也叫“三元组”(triple),这里指的是 64 位的 Linux 系统。x86_64-unknown-linux-gnu 表示编译器将生成适用于 64 位 Linux 系统的代码。

  3. info: syncing channel updates for ‘stable-x86_64-unknown-linux-gnu’
    Rust 的安装器正在同步最新的稳定版(stable channel)更新。这意味着它正在获取当前最新版本的稳定版 Rust 相关组件。

  4. 下载组件(cargo, clippy, rust-docs, rust-std, rustc, rustfmt)

    • cargo 是 Rust 的包管理工具,用于管理依赖和构建项目。
    • clippy 是一个 Rust 代码的静态分析工具,帮助开发者遵循最佳实践。
    • rust-docs 是 Rust 的文档,可以离线访问 Rust 标准库的文档。
    • rust-std 是标准库,Rust 编译时所需要的核心库。
    • rustc 是 Rust 编译器,负责将 Rust 源代码编译为可执行文件。
    • rustfmt 是代码格式化工具,用于确保 Rust 代码风格的一致性。
  5. info: installing component ‘cargo’
    系统开始安装上述下载的组件,如 cargoclippyrust-docs 等。

  6. info: default toolchain set to ‘stable-x86_64-unknown-linux-gnu’
    系统设置了默认的 Rust 工具链为稳定版,即用户将使用稳定版的 Rust 编译器和工具。

  7. Rust is installed now. Great!
    Rust 安装成功,已经准备好使用。

  8. To get started you may need to restart your current shell.
    建议用户重启当前的终端会话,或重新加载终端,以确保 $HOME/.cargo/bin 路径被正确添加到 PATH 环境变量中。

  9. To configure your current shell, you need to source…
    这一行提示如何在当前 shell 中手动加载 Rust 环境,可以运行以下命令:

    . "$HOME/.cargo/env"    # 对于 bash、zsh 等 shell
    

    或者:

    source "$HOME/.cargo/env.fish"  # 对于 fish shell
    

通过这些步骤,Rust 及其工具链已经成功安装,可以开始使用 Rust 开发了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值