rust 快速入门——3 开发环境

362 阅读8分钟

开发环境

普若哥们儿

github.com/wu-hongbing…

gitee.com/wuhongbing/…

开发环境

学习编程语言必须动手实验,先来了解一下 Rust 的开发环境。

Rust SDK 主要提供三个工具 rustccargorustup

Rust 开发工具类比 PythonNodejs 的开发工具:

RustPythonNodejs
编译器rustcpythonnode
包管理工具cargopipnpm
工具链管理工具rustuppyenvnvm

用 rustup -init 在线安装

Other Installation Methods - Rust Forge (rust-lang.org) 下载所需平台的 rustup-init 工具。

rustup-init 下载链接列表的描述格式为 cpu + vendor + OS + ABI。比如目标平台为 x86_64-unknown-linux-gnu,含义为:CPU 是 x86_64,厂商未知,操作系统为 linux,应用程序二进制接口 ABI 为 gnu

以 Windows 平台为例,运行 rustup-init.exe,命令行程序以问答的形式进行安装。默认安装路径为 C:\Users\xxx\.cargoC:\Users\xxx\.rustup

C:\Users\xxx\.rustup 目录结构

C:\Users\xxx\.rustup 目录结构为:

C:\Users\xxx\.rustup
│   settings.toml # 配置文件
│   ...
├───downloads
├───tmp
├───toolchains  # 工具链
│   └───stable-x86_64-pc-windows-msvc  # windows 平台下的工具链
│       ├───bin                        # SDK 工具
│       │       cargo.exe
│       │       rustc.exe
│       │       ...
│       ├───etc
│       │   └───bash_completion.d      # bash下的 rust 命令行工具自动补全工具
│       ├───lib                        # rust 库文件
│       │   └───rustlib
│       │       │   components
│       │       │   manifest-cargo-x86_64-pc-windows-msvc
│       │       │   ...
│       │       ├───etc
│       │       │       intrinsic.natvis
│       │       │       ...
│       │       └───x86_64-pc-windows-msvc
│       │           ├───bin               # 面向 windows 平台的连接工具
│       │           │   │   rust-lld.exe
│       │           │   │   ...
│       │           │   └───gcc-ld
│       │           │           ld.lld.exe
│       │           │           wasm-ld.exe
│       │           │           ...
│       │           └───lib
│       │                   liballoc-cb2478631e21007b.rlib
│       │                   ...
│       ├───libexec
│       │       rust-analyzer-proc-macro-srv.exe
│       │       ...
│       └───share
│           ├───doc
│               ...      
└───update-hashes
        stable-x86_64-pc-windows-msvc
  • C:\Users\xxx\.rustup\toolchains:存放多个版本的工具链,可以看到本例当前只有一个版本的工具链 stable-x86_64-pc-windows-msvc,这是 x86_64-pc-windows-msvc 工具链的稳定版。
  • C:\Users\xxx\.rustup\toolchains\stable-x86_64-pc-windows-msvc\bin:工具链中的工具,最重要的是 cargo.exerustc.exe

C:\Users\xxx\.cargo 目录结构

C:\Users\xxx\.cargo 目录结构为

C:\Users\xxx\.cargo
└───bin
        cargo-clippy.exe
        cargo-fmt.exe
        cargo-miri.exe
        cargo.exe
        clippy-driver.exe
        rls.exe
        rust-analyzer.exe
        rust-gdb.exe
        rust-gdbgui.exe
        rust-lldb.exe
        rustc.exe
        rustdoc.exe
        rustfmt.exe
        rustup.exe

需要在系统 path 环境变量中增加路径 C:\Users\xxx\.cargo\bin。观察 C:\Users\xxx\.cargo\bin 中的文件:

2024/03/18  15:37         9,684,992 cargo-clippy.exe
2024/03/18  15:37         9,684,992 cargo-fmt.exe
2024/03/18  15:37         9,684,992 cargo-miri.exe
2024/03/18  15:37         9,684,992 cargo.exe
2024/03/18  15:37         9,684,992 clippy-driver.exe
2024/03/18  15:37         9,684,992 rls.exe
2024/03/18  15:37         9,684,992 rust-analyzer.exe
2024/03/18  15:37         9,684,992 rust-gdb.exe
2024/03/18  15:37         9,684,992 rust-gdbgui.exe
2024/03/18  15:37         9,684,992 rust-lldb.exe
2024/03/18  15:37         9,684,992 rustc.exe
2024/03/18  15:37         9,684,992 rustdoc.exe
2024/03/18  15:37         9,684,992 rustfmt.exe
2024/03/18  15:37         9,684,992 rustup.exe

可以看到,C:\Users\xxx\.cargo\bin 目录下的所有文件大小相同,实际上文件内容也完全一样。该目录下的可执行文件为代理工具,最终这些代理工具启动的是 .rustup\toolchains 中的工具链中相应的可执行文件,通过这种机制实现多版本的工具链切换。

将任意一个文件改名test.exe,运行 test.exe,可以看到:

C:\Users\xxx\.cargo\bin>test

error: unknown proxy name: 'test'; valid proxy names are 'rustc', 'rustdoc', 'cargo', 'rust-lldb', 'rust-gdb', 'rust-gdbgui', 'rls', 'cargo-clippy', 'clippy-driver', 'cargo-miri', 'rust-analyzer', 'rustfmt', 'cargo-fmt'

这证明 C:\Users\xxx\.cargo\bin 中的可执行文件只是代理,有些命令代理可以直接执行,比如 rustup.exe,有些需要代理到相应版本的工具链的工具,比如 rustc.exe。这些代理程序获取命令行的内容,根据命令行中的 命令字符串 即可知晓需要代理哪个程序。

C:\Users\xxx\.cargo\bin 目录中有 rustup.exe 工具,该工具用于安装和管理 Rust sdk 版本,因此使用 rustup-ini.exe 完成安装后,该 rustup-ini.exe 工具也就不需要了,以后通过 rustup.exe 安装其它版本的工具链即可。正如其名字一样,rustup-ini.exe 仅用于初始安装,安装工具链以及 rustup.exe

使用 rustup-init 工具前,通过设置环境变量 CARGO_HOMERUSTUP_HOME 可以定制安装位置。

  • RUSTUP_HOME 设置了 rustup 的根文件夹(默认为 C:\Users\xxx\.rustup),用于存储已安装的工具链和配置选项。
  • CARGO_HOME 设置了 cargo 工具的根文件夹(默认为 C:\Users\xxx\.cargo)。

注意,还需要将 CARGO_HOME/bin 设置在 PATH 环境变量中。

便携安装

理解了上述内容,可以很容易制作便携版本的 Rust 开发环境:

  1. 在虚拟机中通过 rustup-ini.exe 安装 Rust
  2. 设置环境变量 CARGO_HOMERUSTUP_HOME
  3. 将虚拟机中的 .cargo.rustup 分别拷贝到 CARGO_HOMERUSTUP_HOME 目录。
  4. CARGO_HOME/bin 设置在 PATH 环境变量中

独立安装

Other Installation Methods - Rust Forge (rust-lang.org) 下载完整的独立安装包,可离线安装。Windows 平台安装包为 .msi 格式,Linxu 平台为 .tar.gz 格式。

以 Windows 平台为例,下载 rust-1.76.0-x86_64-pc-windows-msvc.msi,双击即可安装。默认安装目录为 C:\Program Files\Rust stable MSVC 1.76,并自动在系统 path 环境变量中增加路径 C:\Program Files\Rust stable MSVC 1.76\bin

这种安装方式不会在用户目录下生成 .cargo.rustup 目录,也不会安装 rustup 工具,因此不具有版本切换功能,但是开发功能是完整的。

C:\Program Files\Rust stable MSVC 1.76 目录内容为:

C:\Program Files\Rust stable MSVC 1.76
├───bin
│       cargo.exe
│       rustc.exe
│       ...
├───etc
│   └───bash_completion.d
├───lib
│   └───rustlib
│       ├───etc
│       │       ...
│       └───x86_64-pc-windows-msvc
│           ├───bin
│           │   │   rust-lld.exe
│           │   │   ...
│           │   └───gcc-ld
│           │           ld.lld.exe
│           │           ...
│           └───lib
│                   ...
├───libexec
│       rust-analyzer-proc-macro-srv.exe
└───share
    ├───doc
        ...      

主要就是 cargo.exerustc.exe。由于这种安装方式不会安装 rustup 工具,不便于维护,因此不建议在开发中采用。

设置 Rustup 的国内镜像

Rust 工具链也在存储在国外,rustup 工具会自动从服务器上下载最新的版本。也可以配置从国内镜像下载,则需要配置环境变量,比如:

RUSTUP_DIST_SERVER= http://mirrors.ustc.edu.cn/rust-static
RUSTUP_UPDATE_ROOT= http://mirrors.ustc.edu.cn/rust-static/rustup

设置 Cargo 的国内源

Cargo 是 Rust 的包管理工具,在开发工作中,往往需要引入第三方包,Cargo 够根据项目配置文件自动下载所需的第三方包。第三方包存储在国外服务器中,国内也有镜像。如果需要使用国内的镜像,需要在 CARGO_HOME 根目录下配置 config.toml 文件 (没有则新建):

[source.crates-io]
registry = "https://github.com/rust-lang/crates.io-index"
#指定镜像名
replace-with = 'ustc'
# 中国科学技术大学
[source.ustc]
registry = "git://mirrors.ustc.edu.cn/crates.io-index"
[http]
check-revoke = false

安装 MSVC

在 windows 平台部署 Rust 工具链还需要用到 msvc 工具:

  1. 下载 Microsoft C++ 生成工具 Microsoft C++ Build Tools ,Microsoft C++ 生成工具通过可编写脚本的独立安装程序提供 MSVC 工具集,无需使用 Visual Studio。安装时勾选“Desktop development with C++” 选项。Visual Studio 也包含这些工具,

6d146d67a8ea8dac56341c1e484fc215.webp

  1. 为了正常使用 VS 的命令行编译工具,需要设置完整的环境变量,在命令行中运行 C:\Program Files\Microsoft Visual Studio\2022\Community\Common7\Tools\VsDevCmd.bat

更新工具链

运行 rustup update 还可以检查 rustup 的更新,并自动安装最新的版本。

要手动检查更新并安装最新版本的 rustup 而不更新已安装的工具链,请输入 rustup self update

$ rustup self update
info: checking for self-updates
info: downloading self-updates

注意:rustup 在任何工具链安装结束时也会自动更新自己。你可以在运行 rustup updaterustup toolchain install 时,通过传递 --no-self-update 参数来防止这种自动行为。

rustup 命令举例:

命令描述
rustup default nightly将默认的工具链设置为最新的日更版
rustup set profile minimal设置默认的 "profile"
rustup target list列出活动工具链的所有可用目标
rustup target add arm-linux-androideabi安装 Android 目标
rustup target remove arm-linux-androideabi删除 Android 目标
rustup run nightly rustc foo.rs无论活动的工具链如何,都要运行日更版运行
rustc +nightly foo.rs运行日更版编译器的速记方法
rustup run nightly bash运行为日更版编译器配置的外壳
rustup default stable-msvc在 Windows 上,使用 MSVC 工具链而不是 GNU
rustup override set nightly-2015-04-01对于当前目录,使用特定日期的日更版编译器
rustup toolchain link my-toolchain "C:\RustInstallation"通过符号链接现有的安装程序来安装一个自定义的工具链
rustup show显示当前目录下将使用的工具链
rustup toolchain uninstall nightly卸载一个指定的工具链
rustup toolchain help显示一个子命令(如 toolchain)的帮助页面
rustup man cargo(仅适用于 Unix) 查看指定命令(如 cargo)的手册页面