rustup命令行工具使用教程

简介

rustupRust 官方推荐的安装工具和版本管理器,用于安装、管理和更新 Rust 编译器(rustc)、包管理器(cargo)以及其他组件和工具链(toolchains)。

安装 rustup

macOS/Linux 上:

shell 复制代码
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh

Windows 上直接运行 .exe 安装程序(官网:rustup.rs)。

安装完后,默认会将 cargo, rustc, rustup 等可执行文件放在 ~/.cargo/bin 下,并添加到 $PATH

常用命令大全

查看当前使用的 Rust 版本

shell 复制代码
rustup show

或

rustc --version

安装指定版本的 Rust

shell 复制代码
rustup install stable
rustup install beta
rustup install nightly
rustup install 1.86.0  # 安装指定版本

卸载某个版本

shell 复制代码
rustup uninstall nightly

切换全局默认版本

shell 复制代码
rustup default stable
rustup default nightly
rustup default 1.86.0

为某个项目设置局部 Rust 版本

  • 在项目目录里运行:
shell 复制代码
rustup override set nightly

这会在当前目录创建一个 rust-toolchain 文件。

  • 取消 override:
shell 复制代码
rustup override unset

更新 rustup 和所有组件

shell 复制代码
rustup update

只更新某个版本:

shell 复制代码
rustup update nightly

安装和管理组件(如 clippy、rustfmt)

shell 复制代码
rustup component add clippy
rustup component add rustfmt
rustup component list --installed

卸载组件:

shell 复制代码
rustup component remove clippy

添加目标平台(用于交叉编译)

shell 复制代码
rustup target add wasm32-unknown-unknown

查看所有可用 target

shell 复制代码
rustup target list

查看真实路径(shim)

shell 复制代码
rustup which cargo
rustup which rustc

使用某个版本运行命令

shell 复制代码
rustup run nightly cargo build

rust-toolchain.toml(高级用法)

在项目根目录下创建:

toml 复制代码
[toolchain]
channel = "nightly-2024-04-01"
components = ["rustfmt", "clippy"]
targets = ["wasm32-unknown-unknown"]

这个文件可以让项目自动锁定特定版本和组件,非常适合团队协作。

卸载 rustup

shell 复制代码
rustup self uninstall

查看帮助

shell 复制代码
rustup help
rustup help install
相关推荐
盒马盒马12 小时前
Rust:内部可变性 & 型变
开发语言·后端·rust
云水木石15 小时前
Rust 语言开发的 Linux 桌面来了
linux·运维·开发语言·后端·rust
Source.Liu16 小时前
【time-rs】解释://! Invalid format description(error/invalid_format_description.rs)
rust·time
fegggye16 小时前
创建一个rust写的python库[signatures和错误处理]
开发语言·python·rust
Source.Liu16 小时前
【time-rs】解释://! Indeterminate offset(error/indeterminate_offset.rs)
rust·time
福大大架构师每日一题16 小时前
rust 1.92.0 更新详解:语言特性增强、编译器优化与全新稳定API
java·javascript·rust
分布式存储与RustFS19 小时前
MinIO替代方案精选:RustFS深度评测与选型指南
人工智能·rust·开源项目·对象存储·minio·企业存储·rustfs
JPX-NO19 小时前
使用cargo-generate自定义创建项目模板
rust·mvc
JPX-NO19 小时前
Rust Rocket Web 应用项目结构详解(MVC 风格)
rust·mvc·rocket
Source.Liu1 天前
【time-rs】DifferentVariant 错误类型详解(error/different_variant.rs)
rust·time