BT

Rust 1.22 Extends the ? Operator to Option Types

| by Sergio De Simone Follow 14 Followers on Nov 24, 2017. Estimated reading time: 1 minute |

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 Stage
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.

Tell us what you think

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

Email me replies to any of my messages in this thread
Community comments

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

Email me replies to any of my messages in this thread

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

Email me replies to any of my messages in this thread

Discuss

Login to InfoQ to interact with what matters most to you.


Recover your password...

Follow

Follow your favorite topics and editors

Quick overview of most important highlights in the industry and on the site.

Like

More signal, less noise

Build your own feed by choosing topics you want to read about and editors you want to hear from.

Notifications

Stay up-to-date

Set up your notifications and don't miss out on content that matters to you

BT