BT

Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ

Topics

Choose your language

InfoQ Homepage News Rust 1.22 Extends the ? Operator to Option Types

Rust 1.22 Extends the ? Operator to Option Types

Bookmarks

The latest release of Rust stabilizes the usage of ? with Option<T> to simplify option handling. Additionally, it improves compiler performance and backtraces on macOS.

The ? operator was introduced in Rust 1.13 to make it easier to handle Result<T, E>. Previously, to handle results, you could use either pattern matching or the try! operator:

let result = foo();
let mut result = match result {
        Ok(val) => val,
        Err(e) => return Err(e),
}

// Alternatively, you can use the try operator
let mut result = try!(foo());

The ? operator makes legal the following, much shorter syntax:

let mut result = foo()?;
foo()?.bar()?.baz()?

Similarly, you can now write the following syntax with options:

    fn func_returning_option(...)
    let val = func_returning_option(...)?

Instead of:

    match func_returning_option(...) {
        None => ... ,
        Some(val) => ...
    }

Another extension to Rust language syntax allows developers to write:

let mut x = 2;
let y = &8;

// correct in previous Rust versions: x += *y
x += y;

The libbacktrace library has been improved on macOS so it provides filenames and line numbers, which were sorely missing in backtraces on macOS. This is accomplished by replacing the use of dladdr with _NSGetExecutablePath.

A new addition to Rustdoc are compile-fail tests, i.e. tests that succeed if the compiler fail to compile a given statement. For example, you can now define:

/// ```compile_fail
/// let x = 5;
/// x += 2; // shouldn't compile!
/// ```

Tests in Rustdoc are used to ensure that tests included in the documentation are up to date and correct.

On the tooling front, debug compile time has been improved, although the Rust team has not provided any concrete figures about the improvement.

As a final note, Rust 1.22 stabilizes a number of APIs. For a full listing check the detailed release notes.

Rate this Article

Adoption
Style

Hello stranger!

You need to Register an InfoQ account or or login to post comments. But there's so much more behind being registered.

Get the most out of the InfoQ experience.

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Community comments

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

BT