信息来源:
2021 年 10 月 22 日,Rust 1.56.0 新版本发布,同时发布了 Rust 2021 新版次。本次更新主要集中在:prelude 的补录、Cargo 新的默认解析特性 resolver、数组迭代器、闭包中的分离捕获、panic 宏的一致性改进、预留语法、warnings -> errors,宏中的 Or 模式等。实际上,改变并不算大,已有项目的迁移到 2021 版次,也很顺利通畅。
如果你已通过 rustup 安装了 Rust 的早期版本,那么更新到 Rust 1.56.0 相当容易:
$ rustup update stable
如果您还未安装过 Rust,可以从 Rust 官网页面获取 rustup
。
如果官方途径安装速度较慢,可以配置 Rust 工具链的国内源,请参阅《配置 Rust 工具链的国内源》。
Rust 1.56.0 版本和 Rust 2021 版次的升级改进并不算大,新特性大抵如下:
// 2015 or 2018 edition code
let a = SomeStruct::new();
// Move out of one field of the struct
drop(a.x);
// Ok: Still use another field of the struct
println!("{}", a.y);
// Error: Before 2021 edition, tries to capture all of `a`
let c = || println!("{}", a.y);
c();
IntoIterator
: array.into_iter()
现在是按项值遍历,替代了原来的按引用遍历。:pat
中的 A|B
。Cargo.toml
中的 resolver = "2"
,可以删除了。TryInto
、TryFrom
,以及 FromIterator
。println!()
。ident#
、ident"..."
,以及 ident'...'
.bare_trait_objects
和 ellipsis_inclusive_range_patterns
。cargo fix --edition
Cargo.toml
,设定 edition = "2021"
。如:[package]
name = "..."
version = "0.0.1"
...
edition = "2021"
...
cargo build
或 cargo test
因为 Rust 1.56.0 版本和 Rust 2021 版次的升级改进并不多,再者笔者的项目,对于 Rust 稳定版一直追新。所以代码变动非常小,一切过程顺利通畅。
$ cargo fix --edition
Checking ... v0.0.1 (.../backend)
Checking ... v0.0.1 (.../frontend)
warning: `backend\src\main.rs` is already on the latest edition (2021), unable to migrate further
If you are trying to migrate from the previous edition (2018), the
process requires following these steps:
1. Start with `edition = "2018"` in `Cargo.toml`
2. Run `cargo fix --edition`
3. Modify `Cargo.toml` to set `edition = "2021"`
4. Run `cargo build` or `cargo test` to verify the fixes worked
More details may be found at
https://doc.rust-lang.org/edition-guide/editions/transitioning-an-existing-project-to-a-new-edition.html
warning: `backend\...\password.rs` is already on the latest edition (2021), unable to migrate further
warning: `backend\...\chrono.rs` is already on the latest edition (2021), unable to migrate further
warning: `backend\...\markdown.rs` is already on the latest edition (2021), unable to migrate further
...
Finished dev [unoptimized + debuginfo] target(s) in 52.89s
特此分享,谢谢阅读。