如何在Rust工具链之间切换?

104

rustup help toolchain列出以下子命令

SUBCOMMANDS:
    list         List installed toolchains
    install      Install or update a given toolchain
    uninstall    Uninstall a toolchain
    link         Create a custom toolchain by symlinking to a directory
    help         Prints this message or the help of the given subcommand(s)

我已经安装了以下工具链

stable-x86_64-unknown-linux-gnu (default)
nightly-2019-09-05-x86_64-unknown-linux-gnu
nightly-x86_64-unknown-linux-gnu
master

我试图解决rust-clippy的问题,所以我必须安装主要工具链。即使稳定版是我的默认工具链,但我的当前工具链是主要版本,我想切换回稳定版。我如何在不卸载主要工具链的情况下进行操作?

没有切换子命令吗?


据我所知,除非您设置环境变量,否则没有“当前”工具链可用。您可以像这样运行它 cargo +nightly clippy 或者使用默认的工具链。 - PitaJ
@PitaJ,运行 rustup default 将会告诉你 cargo 默认使用的工具链,除非在 .toml 配置文件中另有指定。 - Optimistic Peach
4个回答

130
当然,rustup default stable 命令很有效,但最简单的方法是将 rust-toolchain 文件保存在项目根目录中,类似于 Node.js 项目中的 .nvm 文件。请注意,如果您使用 rust-toolchain.toml 而不是只有 rust-toolchain,则需要添加和 JSON 相似的部分,参见ref。因此,我更喜欢只有简单的 rust-toolchain 文件,它不需要与 Node.js 的 .nvm 文件类似的部分。 rust-toolchain
nightly

带有rust-toolchain文件的IDE截图

或者

stable

2
这似乎是更传统/强大的方法,所以我点了赞。 - Dylan Kerler
3
为什么这个设置被放在一个我从未听说过的奇怪额外文件中,而不是Cargo.toml文件中呢? - mako

63

使用 rustup default <toolchain> 命令来更改默认工具链。您可以使用完整名称(例如 rustup default stable-x86_64-unknown-linux-gnu)或短别名(例如 rustup default stable)。

rustup 还有更加局部化的方法来覆盖默认设置。请参见 rustup 书籍中的Overrides


如果我只想暂时使用其他工具链,例如在我的默认工具链为夜间版时,也可以使用'cargo clippy'吗? - James Ray
3
我找到了答案:https://github.com/rust-lang/rustup#toolchain-override-shorthand。在上述情况下,我可以执行 cargo +stable clippy - James Ray
1
有没有最小的工具链?我只对运行一些命令感兴趣,例如 cargo generate-lockfilecargo tree - n1nsa1d00

63

要在您的存储库中切换夜间版和稳定版配置,请使用:

rustup override set nightly
或者:
rustup override set stable

12

rustup default stable 应该可以工作。这将全局设置 stable 为默认的工具链。

如果要仅针对一个目录/项目设置 stable 作为默认的工具链,使用 rustup override set stable 命令。要取消设置,请使用 rustup override unset


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接