前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Rust 1.56.0 版本和 Rust 2021 版次发布,新特性一览,及项目的迁移、升级

Rust 1.56.0 版本和 Rust 2021 版次发布,新特性一览,及项目的迁移、升级

作者头像
niqin.com
发布2022-09-01 15:50:08
1.6K0
发布2022-09-01 15:50:08
举报
文章被收录于专栏:Rust 生态与实践Rust 生态与实践

信息来源:

  1. Announcing Rust 1.56.0 and Rust 2021
  2. Transitioning an existing project to a new edition
  3. Rust 2021

2021 年 10 月 22 日,Rust 1.56.0 新版本发布,同时发布了 Rust 2021 新版次。本次更新主要集中在:prelude 的补录、Cargo 新的默认解析特性 resolver、数组迭代器、闭包中的分离捕获、panic 宏的一致性改进、预留语法、warnings -> errors,宏中的 Or 模式等。实际上,改变并不算大,已有项目的迁移到 2021 版次,也很顺利通畅。

Rust 1.56.0 和 Rust 2021 的升级或安装

如果你已通过 rustup 安装了 Rust 的早期版本,那么更新到 Rust 1.56.0 相当容易:

代码语言:javascript
复制
$ rustup update stable

如果您还未安装过 Rust,可以从 Rust 官网页面获取 rustup

如果官方途径安装速度较慢,可以配置 Rust 工具链的国内源,请参阅《配置 Rust 工具链的国内源》。

新特性一览

Rust 1.56.0 版本和 Rust 2021 版次的升级改进并不算大,新特性大抵如下:

  • 闭包捕获的改进:直接参考如下示例。
代码语言:javascript
复制
// 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() 现在是按项值遍历,替代了原来的按引用遍历。
  • 宏中的 Or 模式 即 :pat 中的 A|B
  • Cargo 新的默认解析特性 resolver 现在默认值为 2。即 Rust 1.51.0 版本后,显式设定在 Cargo.toml 中的 resolver = "2",可以删除了。
  • prelude 的补录:默认增补 TryIntoTryFrom,以及 FromIterator
  • Panic 宏 期望的输出为字符串格式,就像 println!()
  • 预留语法:ident#ident"...",以及 ident'...'.
  • warnings -> errors:主要涉及 bare_trait_objectsellipsis_inclusive_range_patterns

将已有项目迁移到 Rust 2021 版次

  1. 运行 cargo fix --edition
  2. 编辑 Cargo.toml,设定 edition = "2021"。如:
代码语言:javascript
复制
[package]
name = "..."
version = "0.0.1"
...
edition = "2021"
...
  1. 运行 cargo buildcargo test

因为 Rust 1.56.0 版本和 Rust 2021 版次的升级改进并不多,再者笔者的项目,对于 Rust 稳定版一直追新。所以代码变动非常小,一切过程顺利通畅。

代码语言:javascript
复制
$ 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

特此分享,谢谢阅读。

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2021-10-22,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 Rust 生态与实践 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Rust 1.56.0 和 Rust 2021 的升级或安装
  • 新特性一览
  • 将已有项目迁移到 Rust 2021 版次
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档