有没有更新Cargo到最新官方版本的命令?

202

我似乎有不同版本的rustccargo(我想)。

$ rustc -V
rustc 1.9.0 (e4e8b6668 2016-05-18)
$ cargo -V
cargo 0.10.0-nightly (10ddd7d 2016-04-08)

是否有类似于

的命令?


Is there a command akin to

pip install --upgrade pip 

升级 cargo 的方法?例如:

cargo install --upgrade cargo

1
multirust update. 或者看看 Rustup,但我还没有使用过它。 - starblue
3
multirust 已经被废弃,请使用 https://github.com/rust-lang-nursery/rustup.rs。 - Isan Rivkin
5个回答

244
如果你使用了rustup,rustup update命令足以更新rustccargo。如果你使用的是包管理器或二进制安装程序,请查找这些来源以获取更新。
尽管rustccargo一起发布,但这并不意味着它们的版本需要匹配。事实上,在Rust 1.26.0之前,它们并不匹配,直到更改了Cargo二进制文件以打印Rust版本为止。
我和你使用相同版本的rustccargo,它们对应于Rust 1.9版本发布。没有什么可担心的。
如果你真的想要的话,可以下载最新版Cargo自行编译。只要你的版本在旧版本之前存在于PATH中,它将被使用。
我曾经在本地Rust构建中这样做,以便获得任何版本的Cargo,虽然现在rustup自动使用最新稳定版本中的cargo(当当前工具链中没有可用版本时),这很不错。

3
如果我继承了这个系统,但不知道如何安装 Rust,但需要进行 'rustup update',该怎么办?我可以一整天运行 rustup update,但它不会更新 ~/.cargo/bin/rustc 的版本到刚刚安装的版本。 - cloudsurfin
1
没事了,我当时在一个项目的文件夹里,这个文件夹恰好包含一个 rust-toolchain 文件,覆盖了默认的工具链。 - cloudsurfin

230

简短版:运行命令 rustup update 可以同时更新 Rust 和 Cargo:

$ rustc --version
rustc 1.27.2 (58cc626de 2018-07-18)
$ cargo --version
cargo 1.27.0 (1e95190e5 2018-05-27)

$ rustup update stable
info: syncing channel updates for 'stable-x86_64-unknown-linux-gnu'
info: latest update on 2018-08-02, rust version 1.28.0 (9634041f0 2018-07-30)
info: downloading component 'rustc'
info: downloading component 'rust-std'
info: downloading component 'cargo'
info: downloading component 'rust-docs'
info: removing component 'rustc'
info: removing component 'rust-std'
info: removing component 'cargo'
info: removing component 'rust-docs'
info: installing component 'rustc'
info: installing component 'rust-std'
info: installing component 'cargo'
info: installing component 'rust-docs'

$ rustc --version
rustc 1.28.0 (9634041f0 2018-07-30)
$ cargo --version
cargo 1.28.0 (96a2c7d16 2018-07-13)

29

你还需要更改默认设置:

> rustc --version
rustc 1.41.0 (5e1a79984 2020-01-27)

> rustup update stable

> rustc --version
rustc 1.41.0 (5e1a79984 2020-01-27)

> rustup default stable-x86_64-apple-darwin

> rustc --version
rustc 1.47.0 (18bf6b4f0 2020-10-07)

2
对于像我这样的 Rust 新手,这就是它 - 更改默认设置,仅仅更新是不够的。 - Ilya Luzyanin

12

使用 cargo 更新自身:

cargo install cargo --force

这将重新编译该软件包并安装最新版本。

我决定发布此内容,是因为我发现rustup没有将cargo更新到1.57。


2
虽然有时可能会起作用,但通常情况下这是不应该起作用的,您也不应该能够通过Cargo本身安装不同版本的Cargo:https://github.com/rust-lang/cargo/issues/11266#issuecomment-1286032635 - interfect

0

您可以使用rustup cli编辑您正在使用的cargo和rust版本。您可以指定特定版本或指定通道,如夜间版或测试版。

例如:

rustup override set nightly

1
这并没有回答问题。 - Chayim Friedman

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