BT

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

| by Sergio De Simone Follow 21 Followers on Nov 14, 2016. Estimated reading time: 2 minutes |

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

Rust language soon catches up with Dart language in popularity by Andrew Mezoni

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

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

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

2 Discuss

Educational Content

BT