BT

Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ

Topics

Choose your language

InfoQ Homepage News Rust 1.31 Brings the First Rust 2018 Features, Non-Lexical Lifetimes and Module Improvements

Rust 1.31 Brings the First Rust 2018 Features, Non-Lexical Lifetimes and Module Improvements

This item in japanese

Bookmarks

Rust 1.31 is the first release of the language that implements new features exclusive to Rust 2018 and does not guarantee source compatibility with existing code bases. Rust 2018 is a work in progress and Rust 1.31 only marks the beginning of a three year development cycle that will significantly extend the language.

As InfoQ reported previously, Rust 2018 aims to package under an easily recognizable brand all the changes that have been surfacing to the language since the last major Rust version, Rust 2015. Additionally, Rust 2018 will include a few changes that could potentially break existing source code. This is the case with new keywords such as try, async, await, etc., which could conflict with variable or function names. So being able to choose which language version you are going to use is of great help to preserve your ability to compile existing code bases.

For this approach to work out seamlessly, the Rust team has already included all breaking changes in one go in Rust 1.31, although some are not yet fully implemented. For example, new keywords have been just reserved, albeit still not functional, so developers trying to use them to name variables or functions will get errors. It is important to know that Rust 2015 and 2018 packages can coexist in the same program, meaning a Rust 2018 program can use Rust 2015 packages, and vice versa.

Rust 1.31 introduces a number of major language features, including:

  • Non-lexical lifetimes, which make the Rust borrow checker take into account the actual lifetime of a variable without necessarily bind it to its lexical scope.

      fn main() {
        
        let mut x = 5;
        
        let y = &x;       // In Rust 2018, y lifetime ends here,
        let z = &mut x;   // so this is ok.
      }
    
  • Module path clarity, aiming to simplify the way you use Rust module system and including changes to extern crate usage, streamlining module paths, and more.

  • const fn, which can be used in any constant context and are evaluated at compile time. Constant function may include arithmetic or boolean operations, call other constant functions, etc., but are intrinsically more limited that normal functions. Constant functions are available in Rust 2015 as well.

      const fn foo(x: i32) -> i32 {
        x + 1
      }
      const SIX: i32 = foo(5);
    

The introduction of Rust 2018 does not mean Rust 2015 has reached its end of life, though. In fact, the Rust team plans to backport all changes that are not at risk of breaking source compatibility to Rust 2015, although this will happen at a later point in time.

You can install Rust 1.31 by running rustup update stable. To try out Rust 2018 features, you should specify edition = "2018" in your Cargo.toml file under the [package]. If no edition is provided, the Rust compiler will default to Rust 2015 to maximize source code compatibility.

On a related note, the Rust team has also released its Rust 2018 Survey results, which aims to measure user engagement and satisfaction with Rust, as well as to highlight common shortcomings of the language and tools.

Rate this Article

Adoption
Style

BT