内容简介:Rust 1.40.0 已经正式发布。该版本的亮点包括有 #[non_exhaustive] 和 macros!() and #[attribute]s 的改进。 具体更新内容如下: #[non_exhaustive] 结构,枚举和变体 当属性#[non_exhaustive]附加到struct或的变...#########
Rust 1.40.0 已经正式发布。该版本的亮点包括有 #[non_exhaustive] 和 macros!() and #[attribute]s 的改进。
具体更新内容如下:
#[non_exhaustive] 结构,枚举和变体
当属性#[non_exhaustive]附加到struct或的变体时enum,它将防止定义它的板条箱外部的代码构造所述struct或变体。为了避免将来损坏,还防止其他包装箱在田地上进行彻底匹配。以下示例说明了beta取决于的错误alpha:
// alpha/lib.rs:
#[non_exhaustive]
struct Foo {
pub a: bool,
}
enum Bar {
#[non_exhaustive]
Variant { b: u8 }
}
fn make_foo() -> Foo { ... }
fn make_bar() -> Bar { ... }
// beta/lib.rs:
let x = Foo { a: true }; //~ ERROR
let Foo { a } = make_foo(); //~ ERROR
// `beta` will still compile when more fields are added.
let Foo { a, .. } = make_foo(); //~ OK
let x = Bar::Variant { b: 42 }; //~ ERROR
let Bar::Variant { b } = make_bar(); //~ ERROR
let Bar::Variant { b, .. } = make_bar(); //~ OK
// -- `beta` will still compile...
幕后发生的事情是,#[non_exhaustive] struct或的构造函数的可见性enum降低到pub(crate),从而阻止了在定义它的板条箱外部进行访问。
更重要的方面是,#[non_exhaustive]也可以附加到enum自身上。从标准库中获取的示例是Ordering:
#[non_exhaustive]
pub enum Ordering { Relaxed, Release, Acquire, AcqRel, SeqCst }
#[non_exhaustive]在此上下文中的目的是确保可以随时间添加更多变体。这是通过防止其他包装箱从详尽模式实现match-ing上Ordering。也就是说,编译器将拒绝:
match ordering {
// This is an error, since if a new variant is added,
// this would suddenly break on an upgrade of the compiler.
Relaxed | Release | Acquire | AcqRel | SeqCst => {
/* logic */
}
}
取而代之的是,其他板条箱需要通过添加通配符来解决更多变体的可能性,例如_:
match ordering {
Relaxed | Release | Acquire | AcqRel | SeqCst => { /* ... */ }
// OK; if more variants are added, nothing will break.
_ => { /* logic */ }
}
有关该#[non_exhaustive]属性的更多详细信息,可参见稳定性报告。
Macro and attribute 的改进
例如,用户可以编写以下类型:Foo = expand_to_type!(bar); 其中 expand_to_type 将是一个 procedural macro。
包括有bang!() macros, 例如:
macro_rules! make_item { ($name:ident) => { fn $name(); } }
extern {
make_item!(alpha);
make_item!(beta);
}
Procedural macro attributes on items in extern { ... } blocks 现在也被支持:
extern "C" {
// Let's assume that this expands to `fn foo();`.
#[my_identity_macro]
fn foo();
}
目前,函数式(mac!())和属性(#[mac])macros 都可以生成macro_rules!项目。
也就是说,以下内容现在有效:
macro_rules! accept_meta { ($m:meta) => {} }
accept_meta!( my::path );
accept_meta!( my::path = "lit" );
accept_meta!( my::path ( a b c ) );
accept_meta!( my::path [ a b c ] );
accept_meta!( my::path { a b c } );
标准库中增加的 const fn
此版本中,以下函数成为const fn:
增加到标准库的函数
以下函数和宏已经稳定:
-
{f32,f64}::to_be_bytes,{f32,f64}::to_le_bytes,{f32,f64}::to_ne_bytes,{f32,f64}::from_be_bytes,{f32,f64}::from_le_bytes,和{f32,f64}::from_ne_bytes
详情可查看更新说明:
https://blog.rust-lang.org/2019/12/19/Rust-1.40.0.html
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网
猜你喜欢:- NPM包(模块)发布、更新、撤销发布
- 有赞灰度发布与蓝绿发布实践
- 【重磅发布】Linkis 0.10.0 版本发布
- BeetlSQL 3.0.9 发布,Idea 插件发布
- 贝密游戏 0.7.0 发布,发布斗地主
- 【重磅发布】DataSphere Studio 0.9.0 版本发布
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
精彩绝伦的CSS
[美] Eric A. Meyer / 姬光 / 人民邮电出版社 / 2012-7 / 49.00元
内容简介: 打造现代布局的专业技术 本书远非只是介绍基础知识,它不仅全面细致地讲解布局与效果,而且展望了HTML5和CSS3的未来。业内很少有人能像Eric A. Meyer一样详细阐明CSS,他在本书中深入分析了普遍适用的实用技术,讲解了如何选用正确的工具、如何通过jQuery使用CSS效果和CSS3技术。 本书主要内容如下: 显示或隐藏元素 通过XHTML为bod......一起来看看 《精彩绝伦的CSS》 这本书的介绍吧!