BT

InfoQ Homepage News Rust 1.13 Brings New ? Error Handling Operator, Performance Improvements, and More

Rust 1.13 Brings New ? Error Handling Operator, Performance Improvements, and More

Bookmarks

For version 1.13, the Rust core team has focused on improving the language syntax for error handling by introducing a new ? operator, optimizing compile times, and adding support for type level macros.

Rust new ? operator makes it easier to handle errors when the local policy is propagating the error up the caller chain. In other words, if you have a function that can return an error, you call it like in the following snippet to take care of the possibility of it failing:

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 new ? operator can be seen as a shorthand for the try syntax shown above, which can be more succinctly written as:

let mut result = foo()?;

The benefit of the new syntax is more evident when function calls are chained, like here:

try!(try!(try!(foo()).bar()).baz())

// becomes:
foo()?.bar()?.baz()?

The ? operator is defined as a Rust macro and is an example of how Rust can provide new syntax without affecting the underlying features of the language, according to the Rust core team.

Rust 1.13 also brings a couple of new optimizations to the compiler that can speed up compile times up to 50% in several cases. This is nicely represented in the following diagram representing the evolution of Rust benchmarks compile times over the 1.13 development cycle.

The sharp drop in compile times for two benchmarks on September, 1 comes from improved caching of associated types, which were previously computed each time they were required.

Another improvement to the compiler comes from the optimized handling of function inlining, which is carried through only for the crates that actually call inlined functions. An extreme example of the benefit this change brings is provided by the not yet released winapi–0.3 crate, for which rustc only produce the intermediate MIR representation for other crates to use and emits no machine code at all.

More compiler optimizations can be found here.

Other notable changes brought by Rust 1.13 are:

  • Cargo has been updated to include recent security patches for curl and OpenSSL.

  • macros can be used in type position, as in the following example:

    
    macro_rules! Tuple { { $A:ty,$B:ty } => { ($A, $B) } }
    let x: Tuple!(i32, i32) = (1, 2);
    
    This also allows to express recursion and choice when doing type level programming (TLP), i.e. computing types.
  • attributes can now be applied to statements, as in the following example:
    
    // Apply a lint attribute to a single statement
    #[allow(uppercase_variable)] let BAD_STYLE = List::new(); 
    
    Besides lint attributes, developers can specify conditional compilation attributes and custom attributes meant to act as annotations for external tools.

Developers targeting the ARM platform should be aware that Rust 1.13 includes a serious code generation bug and should aim to use the 1.14 betas.

With more than 150 contributors, Rust 1.13 includes many more changes that you can read about in the release notes.

Rate this Article

Adoption
Style

Educational Content

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

  • Rust language soon catches up with Dart language in popularity

    by Andrew Mezoni /

    Your message is awaiting moderation. Thank you for participating in the discussion.

    As said Kasper Lund: "Dart is the fastest growing language at Google".
    I just I want to add - if the Dart is the fastest growing language at Google, the Dart will eventually become the fastest growing language at World.
    And Rust will be the next after Dart, because Rust is great, but Dart is the greatest.

  • Re: Rust language soon catches up with Dart language in popularity

    by Michal Kunikowski /

    Your message is awaiting moderation. Thank you for participating in the discussion.

    I think you can't compare Dart to Rust as former has GC.
    In Rust you have full control of memory management so it can be used where c/c++ is used.

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

Is your profile up-to-date? Please take a moment to review and update.

Note: If updating/changing your email, a validation request will be sent

Company name:
Company role:
Company size:
Country/Zone:
State/Province/Region:
You will be sent an email to validate the new email address. This pop-up will close itself in a few moments.