歡迎您光臨本站 註冊首頁

Rust 1.40.0 發布

←手機掃碼閱讀     admin @ 2019-12-22 , reply:0

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

增加到標準庫的函數

以下函數和宏已經穩定:

詳情可查看更新說明:

https://blog.rust-lang.org/2019/12/19/Rust-1.40.0.html


[admin ]

來源:OsChina
連結:https://www.oschina.net/news/112235/rust-1-40-0-released
Rust 1.40.0 發布已經有169次圍觀

http://coctec.com/news/all/show-post-221495.html