在安装 Rust
时,经常会卡在下载那里。因为服务器在国外,难免会有延迟。我们可以在命令窗口执行如下指令,使当前会话的 Rust
服务器地址替换为国内的源。这里使用中科大的源。
[wlb@Arco ~]$ export RUSTUP_DIST_SERVER=https://mirrors.ustc.edu.cn/rust-static
[wlb@Arco ~]$ export RUSTUP_UPDATE_ROOT=https://mirrors.ustc.edu.cn/rust-static/rustup
然后在执行如下指令,获取安装脚本并运行,有提问就默认回车:
[wlb@Arco ~]$ 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/wlb/.rustup
This can be modified with the RUSTUP_HOME environment variable.
The Cargo home directory located at:
/home/wlb/.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/wlb/.cargo/bin
This path will then be added to your PATH environment variable by
modifying the profile files located at:
/home/wlb/.profile
/home/wlb/.bash_profile
/home/wlb/.bashrc
/home/wlb/.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-unknown-linux-gnu
default toolchain: stable (default)
profile: default
modify PATH variable: yes
1) Proceed with installation (default)
2) Customize installation
3) Cancel installation
>
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'
info: latest update on 2021-06-17, rust version 1.53.0 (53cb7b09b 2021-06-17)
info: downloading component 'cargo'
6.0 MiB / 6.0 MiB (100 %) 5.6 MiB/s in 1s ETA: 0s
info: downloading component 'clippy'
info: downloading component 'rust-docs'
16.1 MiB / 16.1 MiB (100 %) 5.8 MiB/s in 2s ETA: 0s
info: downloading component 'rust-std'
25.3 MiB / 25.3 MiB (100 %) 6.5 MiB/s in 3s ETA: 0s
info: downloading component 'rustc'
48.4 MiB / 48.4 MiB (100 %) 5.6 MiB/s in 8s ETA: 0s
info: downloading component 'rustfmt'
info: installing component 'cargo'
info: installing component 'clippy'
info: installing component 'rust-docs'
16.1 MiB / 16.1 MiB (100 %) 12.8 MiB/s in 1s ETA: 0s
info: installing component 'rust-std'
25.3 MiB / 25.3 MiB (100 %) 14.6 MiB/s in 1s ETA: 0s
info: installing component 'rustc'
48.4 MiB / 48.4 MiB (100 %) 16.4 MiB/s in 2s 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.53.0 (53cb7b09b 2021-06-17)
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
最终你会看到这一句 Rust is installed now. Great!
表示已经安装完成。
安装脚本会默认安装一整套工具链,所以你可以使用 cargo
命令尽情地玩耍了。
[wlb@Arco ~]$ cargo --help
Rust package manager
USAGE:
cargo [+toolchain] [OPTIONS] [SUBCOMMAND]
OPTIONS:
-V, --version Print version info and exit
--list List installed commands
--explain <CODE> Run `rustc --explain CODE`
-v, --verbose Use verbose output (-vv very verbose/build.rs output)
-q, --quiet No output printed to stdout
--color <WHEN> Coloring: auto, always, never
--frozen Require Cargo.lock and cache are up to date
--locked Require Cargo.lock is up to date
--offline Run without accessing the network
--config <KEY=VALUE>... Override a configuration value (unstable)
-Z <FLAG>... Unstable (nightly-only) flags to Cargo, see 'cargo -Z help' for details
-h, --help Prints help information
Some common cargo commands are (see all commands with --list):
build, b Compile the current package
check, c Analyze the current package and report errors, but do not build object files
clean Remove the target directory
doc Build this package's and its dependencies' documentation
new Create a new cargo package
init Create a new cargo package in an existing directory
run, r Run a binary or example of the local package
test, t Run the tests
bench Run the benchmarks
update Update dependencies listed in Cargo.lock
search Search registry for crates
publish Package and upload this package to the registry
install Install a Rust binary. Default location is $HOME/.cargo/bin
uninstall Uninstall a Rust binary
See 'cargo help <command>' for more information on a specific command.
初学者最常用的就是这三个:
cargo new project_name //新建一个 Rust 项目
cargo build //编译项目
cargo run //运行程序(如果没编译,会先编译再运行)