BT

Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ

Topics

Choose your language

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

This item in japanese

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

BT