Rust 1.42.0 release: slice templates and more convenient panic messages

The Rust team is pleased to announce the release of a new version, 1.42.0. Rust is a programming language that allows everyone to create reliable and efficient software.


If you installed the previous version of Rust using tools rustup, then to upgrade to version 1.42.0 you just need to run the following command:


rustup update stable

If you haven’t already installed it rustup, you can install it from the corresponding page of our website, as well as see detailed release notes on GitHub.


What is included in the stable version 1.42.0


The main innovations of Rust 1.42.0 are more convenient panic messages in case of a call unwrap, slice templates, declaring obsolete Error::descriptionand much more. See release notes for more information.


Panic messages from Optionand Resultnow include useful line numbers


In Rust 1.41.1, a call unwrap()to a value Option::Nonegenerated an error message similar to this:


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

Similarly, the line numbers in panic messages generated by calls unwrap_err, expectand expect_err, like the corresponding methods for the type Result, also referred to internals core.


In Rust 1.42.0, all of these eight functions generate panic messages that include the line numbers from where they were called. New posts look something like this:


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

This means that the erroneous call unwrapwas on line 2 in the file 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