关于 Rust
- Rust 由工具 rustup 安装和管理,rustup 工具可以管理多个 rust 版本 和 cargo
- 如果您曾经安装过 rustup,可以执行 rustup update 来升级 Rust
- 在 Rust 开发环境中,所有工具都安装在 ~/.cargo/bin 目录中,您可以在这里找到包括 rustc、cargo 和 rustup 在内的 Rust 工具链。
- 卸载rust:
rustup self uninstall
- 官网
https://www.rust-lang.org - 官方文档
https://www.rust-lang.org/zh-CN/learn - Github
https://github.com/rust-lang/rust
其它教程资源等
- 官方文档中文版 非官方翻译
https://kaisery.github.io/trpl-zh-cn/ - Rust 中文资源
https://rustwiki.org/
https://github.com/rust-lang-cn - 通过例子学 Rust
https://rustwiki.org/zh-CN/rust-by-example/ - RUST 标准库
https://rustwiki.org/zh-CN/std/ - Cargo 手册
https://rustwiki.org/zh-CN/cargo/
Linux 安装 rust
# 设置环境变量,指定国内地址
export RUSTUP_DIST_SERVER=https://mirrors.ustc.edu.cn/rust-static
export RUSTUP_UPDATE_ROOT=https://mirrors.ustc.edu.cn/rust-static/rustup
# 下载脚本、执行安装
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
使环境变量生效
source "$HOME/.cargo/env"
rustc -V
macOS 安装 rust
# 安装 rustup
$ brew install rustup
# rustup 初始化会安装 rust 和 cargo
$ rustup-init
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:
/Users/xx/.rustup # Rustup 元数据和工具安装地址
This can be modified with the RUSTUP_HOME environment variable.
The Cargo home directory located at:
/Users/xx/.cargo # 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:
# cargo 执行地址,本质是对 /usr/local/bin/rustup-init 的软链接
/Users/xx/.cargo/bin
This path will then be added to your PATH environment variable by
modifying the profile files located at:
/Users/xx/.profile
/Users/xx/.bash_profile
/Users/xx/.zshenv
You can uninstall at any time with rustup self uninstall and
these changes will be reverted.
Current installation options:
default host triple: x86_64-apple-darwin
default toolchain: stable (default)
profile: default
modify PATH variable: yes
1) Proceed with installation (default)
2) Customize installation
3) Cancel installation
>
...
stable-x86_64-apple-darwin updated - rustc 1.60.0 (7737e0b5c 2022-04-04) (from rustc 1.40.0 (73528e339 2019-12-16))
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, run:
source $HOME/.cargo/env
- 你也可以通过
brew install rust
安装,但只会安装 rust,还需要额外安装 cargo 等。 - 使用 brew 安装方式, 升级 rustup 需要使用 brew。使用
rustup self update
可能无效。 - 卸载 rustup 可以使用 rustup 提供的命令。
- rustc, rust-std, cargo, rust-docs, rust-std’ , rustfmt, rust-std , rust-std, rust-src’
- 你可能要重启终端窗口(或新开一个 tab),让环境变量生效,才能运行安装好的这些命令。
安装成功,测试/查看版本
# 安装成功,测试/查看版本
$ cargo --version
cargo 1.60.0 (d1fd9fe2c 2022-03-01)
$ rustc --version
rustc 1.60.0 (7737e0b5c 2022-04-04)
# 查看 rustup 版本
$ rustup --version
rustup 1.21.1 (7832b2ebe 2019-12-20)
安装多版本
# 安装 1.59 版本的 rust
$ rustup install 1.59
info: syncing channel updates for '1.59-x86_64-apple-darwin'
...
1.59-x86_64-apple-darwin installed - rustc 1.59.0 (9d1b2106e 2022-02-23)
# 查看已安装的rust版本
$ rustup toolchain list
stable-x86_64-apple-darwin (default)
nightly-x86_64-apple-darwin
1.59-x86_64-apple-darwin # 刚才安装的版本
# 查看当前版本
$ rustc --version
rustc 1.60.0 (7737e0b5c 2022-04-04)
# 切换版本为 1.59
$ rustup default 1.59
...
1.59-x86_64-apple-darwin unchanged - rustc 1.59.0 (9d1b2106e 2022-02-23)
$ rustc --version
rustc 1.59.0 (9d1b2106e 2022-02-23)
安装组件
假设当前 rust 为 1.59 版本
会被安装到目录: ~/.rustup/toolchains/1.59-x86_64-apple-darwin/lib/rustlib
# 安装 rust-src
# 这个组件安装地址为:/Users/luyi/.rustup/toolchains/1.59-x86_64-apple-darwin/lib/rustlib/src
$ rustup component add rust-src
# 移除
$ rustup component remove rust-src
查看命令的使用
$ rustup component -h
rustup-component
Modify a toolchain's installed components
USAGE:
rustup component <SUBCOMMAND>
FLAGS:
-h, --help Prints help information
SUBCOMMANDS:
list List installed and available components
add Add a component to a Rust toolchain
remove Remove a component from a Rust toolchain
help Prints this message or the help of the given subcommand(s)
Hello Rust
创建文件 a.rs
fn main() {
println!("Hello Rust!");
}
编译、执行
# 编译,生成可执行文件 a
$ rustc a.rs
# 执行程序
$ ./a
Hello Rust!
使用编译器设置编译地址时,可以查询 rustc 完整路径
$ which rustc
/Users/xx/.cargo/bin/rustc
2022-05-16(一)