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
相关推荐
techdashen21 小时前
你想在 Rust 中实现动态库热重载?
开发语言·chrome·rust
只会写bug的靓仔1 天前
我把 Electron+Go 的 LOL 战绩工具重写成 Tauri+Rust,安装包从 128 MB 砍到 5 MB
golang·rust·electron
本地化文档1 天前
sphinxcontrib-rust-docs-l10n
python·rust·github·gitcode·sphinx
韦胖漫谈IT1 天前
选语言不是站队,是选适合问题的工具
java·python·ai·rust·go·技术落地
特立独行的猫a1 天前
鸿蒙 PC 平台 Rust 语言第三方库与应用移植全景指南
华为·rust·harmonyos·三方库·鸿蒙pc
红尘散仙1 天前
我把终端小说阅读器接上了 AI Agent:TRNovel 现在能用 skill 生成书源了
人工智能·后端·rust
peterfei2 天前
给 AI Agent 装上"长期记忆":一个 200 行 Rust 库解决 LLM 的致命短板
rust
SOC罗三炮2 天前
OpenHuman 源码深度解构:一个 Rust 驱动的本地优先 AI 个人助手
开发语言·人工智能·rust
techdashen2 天前
Rust 中的小字符串:smol_str 与 smartstring 的对决
开发语言·后端·rust
福大大架构师每日一题2 天前
rust 1.96.0 更新:语言、编译器、Cargo、Rustdoc、兼容性全面升级,必看完整解读
android·开发语言·rust