Rust 1.42.0版本:切片模板和更方便的紧急消息

Rust团队很高兴地宣布发布了一个新版本1.42.0。Rust是一种编程语言,它使每个人都可以创建可靠而高效的软件。


如果您使用tools安装了Rust的先前版本rustup,那么要升级到1.42.0版本,您只需运行以下命令:


rustup update stable

如果您尚未安装它rustup,您可以安装它从我们的网站的相应页面,也可以查看详细的发行说明在GitHub上。


稳定版1.42.0中包含什么


Rust 1.42.0的主要创新是在调用unwrap,切片模板,声明过时Error::description情况下更方便的紧急消息有关更多信息,请参见发行说明。


恐慌的消息OptionResult现在包括有用的行号


在Rust 1.41.1中,unwrap()对值的调用Option::None生成类似于以下内容的错误消息:


thread 'main' panicked at 'called `Option::unwrap()` on a `None` value', /.../src/libcore/macros/mod.rs:15:40

类似地,在由呼叫产生恐慌消息的行号unwrap_errexpectexpect_err,该类型相应的方法等Result,也称为内部core


在Rust 1.42.0中,所有这八个函数都会生成紧急消息,其中包括调用它们的行号。新帖子看起来像这样:


thread 'main' panicked at 'called `Option::unwrap()` on a `None` value', src/main.rs:2:5

这意味着错误的调用unwrap在文件的第2行上src/main.rs


#[track_caller]. Rust, , .



Rust 1.26 " ", . :


fn foo(words: &[&str]) {
    match words {
        [] => println!("empty slice!"),
        [one] => println!("one element: {:?}", one),
        [one, two] => println!("two elements: {:?} {:?}", one, two),
        _ => println!("I'm not sure how many elements!"),
    }
}

, . , , .


Rust 1.42 :


fn foo(words: &[&str]) {
    match words {
        ["Hello", "World", "!", ..] => println!("Hello World!"),
        ["Foo", "Bar", ..] => println!("Baz"),
        rest => println!("{:?}", rest),
    }
}

.. " " (rest pattern), . , :


fn foo(words: &[&str]) {
    match words {
        //  ,    ,    "!".
        [.., "!"] => println!("!!!"),

        // `start` -     ,  ,    "z".
        [start @ .., "z"] => println!("starts with: {:?}", start),

        // `end` -     ,  ,    "a".
        ["a", end @ ..] => println!("ends with: {:?}", end),

        rest => println!("{:?}", rest),
    }
}

, Inside Rust , ! .


matches!


, matches!, , true, . :


//   match:
match self.partial_cmp(other) {
    Some(Less) => true,
    _ => false,
}

//   `matches!`:
matches!(self.partial_cmp(other), Some(Less))

| if:


let foo = 'f';
assert!(matches!(foo, 'A'..='Z' | 'a'..='z'));

let bar = Some(4);
assert!(matches!(bar, Some(x) if x > 2));

use proc_macro::TokenStream;


Rust 2018 extern crate. - , , , - extern crate proc_macro;.


, Cargo, 2018, ; use . , use proc_macro::TokenStream;, , extern crate proc_macro; . , .




API




Rust 1.42.0: Rust Cargo.



: 32- Apple 3.


Error::description


. Error::description . :


fn description(&self) -> &str

description &str, , . Error ; , , , : String. , Rust , Display/Debug, .


API Rust 1.0. : Rust 1.27, " ". , . , Error. , , Error. , description , — , .


32- Apple


Apple 32- , . 3. , .


1.42.0


, Rust 1.42.0. , !



Rust - .


andreevlex, funkill, Hirrolot nlinker.


All Articles