Mac OS X
使用包管理器 Homebrew 安装 rustup(rust toolchain 安装管理器)
>brew install rustup
Please run `rustup-init` to initialize `rustup` and install other Rust components.
运行 rustup-init 命令安装 Rust 环境 (包括 rustc、cargo、rustup 等工具):
>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/brian/.rustup
This can be modified with the RUSTUP_HOME environment variable.
The Cargo home directory is located at:
/Users/brian/.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:
/Users/brian/.cargo/bin
This path will then be added to your PATH environment variable by
modifying the profile files located at:
/Users/brian/.profile
/Users/brian/.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 standard installation (default - just press enter)
2) Customize installation
3) Cancel installation
# 一般地直接回车默认即可
info: profile set to 'default'
info: default host triple is x86_64-apple-darwin
info: syncing channel updates for 'stable-x86_64-apple-darwin'
info: latest update on 2024-04-09, rust version 1.77.2 (25ef9e3d8 2024-04-09)
info: downloading component 'cargo'
7.0 MiB / 7.0 MiB (100 %) 2.3 MiB/s in 3s ETA: 0s
info: downloading component 'clippy'
info: downloading component 'rust-docs'
14.9 MiB / 14.9 MiB (100 %) 4.1 MiB/s in 3s ETA: 0s
info: downloading component 'rust-std'
25.4 MiB / 25.4 MiB (100 %) 4.1 MiB/s in 6s ETA: 0s
info: downloading component 'rustc'
54.9 MiB / 54.9 MiB (100 %) 4.4 MiB/s in 15s ETA: 0s
info: downloading component 'rustfmt'
info: installing component 'cargo'
info: installing component 'clippy'
info: installing component 'rust-docs'
14.9 MiB / 14.9 MiB (100 %) 4.8 MiB/s in 2s ETA: 0s
info: installing component 'rust-std'
25.4 MiB / 25.4 MiB (100 %) 14.0 MiB/s in 1s ETA: 0s
info: installing component 'rustc'
54.9 MiB / 54.9 MiB (100 %) 15.5 MiB/s in 3s ETA: 0s
info: installing component 'rustfmt'
info: default toolchain set to 'stable-x86_64-apple-darwin'
stable-x86_64-apple-darwin installed - rustc 1.77.2 (25ef9e3d8 2024-04-09)
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):
. "$HOME/.cargo/env" # For sh/bash/zsh/ash/dash/pdksh
source "$HOME/.cargo/env.fish" # For fish
根据提示配置shell终端环境变量:
>source "$HOME/.cargo/env"
安装完成后,运行以下命令来测试 Rust 环境是否安装成功:
>rustc --version
rustc 1.77.2 (25ef9e3d8 2024-04-09)
>cargo --version
cargo 1.77.2 (e52e36006 2024-03-26)
>rustfmt --version
rustfmt 1.7.0-stable (25ef9e3d 2024-04-09)
如果安装成功,将会显示 rustc、cargo、rustfmt 的版本号。
Windows
下载 Rustup-init.exe 安装包,然后运行安装程序。
安装完成后,运行以下命令来测试 Rust 环境是否安装成功:
>rustc --version
如果安装成功,将会显示 rustc 的版本号。
Linux
安装 Rust 环境:
>curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
根据提示配置shell终端环境变量:
>. "$HOME/.cargo/env"
# env命令会将.cargo/bin加入到系统的path中
运行env命令等价于下述方式进行环境变量配置:
# 配置rust环境变量(推荐使用profile.d目录)
vi /etc/profile.d/rust.sh
export RUSTUP_HOME=$HOME/.rustup
export CARGO_HOME=$HOME/.cargo
export PATH=$CARGO_HOME/bin:$PATH
#使改动生效,可以通过运行source ~/.profile或重新登录shell
>source ~/.profile
安装或配置完成后,运行以下命令来测试 Rust 环境是否安装成功:
>rustc --version #rust编译器版本
>rustup --version #rustup工具链管理器版本
>cargo --version #rust包管理器版本
vscode 安装rust插件
安装 Visual Studio Code 后,安装必须的拓展:
- Rust-Analyzer - 用于 Rust 代码自动补全、语法检查、代码格式化等功能。
- CodeLLDB - 用于调试c++, rust代码
其他可能感兴趣的扩展:
- Catppuccin Noctis -非常漂亮的一款主题
- crates - 使用Cargo.toml文件管理Rust的的依赖。
- Error Lens- 更直观的错误提示
- GitLens - 增强Git能力的插件
其他扩展: Even Better TOML,支持 .toml 文件完整特性 Project Manager(收藏、识别和管理你的工作空间和项目文件夹)
rustup 常用命令
>rustup --version # 查看 rustup 版本
> rustup -V
rustup 1.27.0 (2024-03-08)
> rustup update # 更新 rustup 到最新版本
> rustup target list # 查看可用的目标
> rustup target add <target> # 添加目标
> rustup component list # 查看可用的组件
> rustup component add <component> # 安装组件
> rustup component remove <component> # 卸载组件
> rustup self update # 更新 rustup 自身
> rustup self uninstall # 卸载 rustup 自身
cargo 常用命令
> cargo --version # 查看 cargo 版本
> cargo -V
cargo 1.77.2 (e52e36006 2024-03-26)
> cargo --help # 查看 cargo 命令的帮助
>cargo new rust_101 # Create a new cargo package,其中rust_101为项目名称
>cd rust_101
>cargo run # 运行项目,表面上一气呵成,实际上首先对项目进行编译然后再运行
Compiling rust_101 v0.1.0 (/Users/brian/wks_rust/rust_101)
Finished dev [unoptimized + debuginfo] target(s) in 2.73s
Running `target/debug/rust_101`
Hello, world!
>cargo build # 先编译项目,默认是debug模式,编译后生成可执行文件
Finished dev [unoptimized + debuginfo] target(s) in 0.00s
>ls ./target/debug/
build deps examples incremental rust_101 rust_101.d
>./target/debug/rust_101 #再运行可执行程序
Hello, world!
>cargo build --release # 编译release版本
Compiling rust_101 v0.1.0 (/Users/brian/wks_rust/rust_101)
Finished release [optimized] target(s) in 1.27s
>ls ./target/
CACHEDIR.TAG debug release
> ./target/release/rust_101 #运行release版本
Hello, world!
>cargo check # 检查项目,快速的检查一下代码能否编译通过,但不会生成可执行文件
>cargo test # 测试项目
>cargo doc # 生成项目文档
>cargo clean # 清理项目
>cargo update # 更新 cargo 依赖
>cargo search <package> # 搜索 cargo 包
>cargo install <package> # 安装 cargo 包
cargo教程:https://course.rs/first-try/cargo.html
Cargo.toml 和 Cargo.lock 是 cargo 的核心文件,它的所有活动均基于此二者。
Cargo.toml 是 cargo 特有的项目数据描述文件。它存储了项目的所有元配置信息,如果 Rust 开发者希望 Rust 项目能够按照期望的方式进行构建、测试和运行,那么,必须按照合理的方式构建 Cargo.toml。
Cargo.lock 文件是 cargo 工具根据同一项目的 toml 文件生成的项目依赖详细清单,因此我们一般不用修改它,只需要对着 Cargo.toml 文件撸就行了。
什么情况下该把 Cargo.lock 上传到 git 仓库里?很简单,当你的项目是一个可运行的程序时,就上传 Cargo.lock,如果是一个依赖库项目,那么请把它添加到 .gitignore 中。