BT

Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ

Topics

Choose your language

InfoQ Homepage News Rust Has Got Existential Types

Rust Has Got Existential Types

This item in japanese

Bookmarks

Version 1.26 of Rust adds support for existential types, improved match bindings, slice patterns, and some useful syntactic sugar. The Rust compiler has also become faster and supports 128 bit integers.

Existential types are implemented through impl Trait. This allows developers to specify a return type from a function without actually saying which concrete type it is. For example:

fn foo() -> impl Trait {
    // ...
}

In the snippet above, foo is declared as a function that returns a type that implements the Trait trait without giving the concrete type. This is somewhat equivalent to the following declaration:

fn foo() -> Box<Trait> {
    // ...
}

Though, using Box<Trait> implies dynamic allocation, which is not always desirable or required. Instead impl Trait guarantees static dispatch. This requires foo to only return results of a same type. Additionally, impl Trait syntax has less glue, as the following example shows:

trait Trait {
    fn method(&self);
}

impl Trait for i32 {
    // implementation goes here
}

impl Trait for f32 {
    // implementation goes here
}

fn new_foo() -> impl Trait {
    5  // we can just return an i32 here
}

fn old_foo() -> Box<Trait> {
    Box::new(5) as Box<Trait>  // this is cumbersome
}

The new impl Trait syntax shines when it comes to defining functions that return a closure, which implement the Fn trait:

fn foo() -> impl Fn(i32) -> i32 {
    |x| x + 1
}

The impl Trait syntax can also be used as a replacement for a generic type declaration, such as in the following example, although in this case it defines a universal type and not an existential type:

// before
fn foo<T: Trait>(x: T) {

// after
fn foo(x: impl Trait) {

Another improvement that will make life easier for both experienced and new Rust programmers is smarter match bindings, that require less understanding of the compiler internals. For example, the following code is now legal:

fn hello(arg: &Option<String>) {
    match arg {
        Some(name) => println!("Hello {}!", name),
        None => println!("I don't know who you are."),
    }
}

In previous Rust versions, you should have added some boilerplate to make the compiler happy, although there was no ambiguity as to your matching intentions:

    match arg {
        &Some(ref name) => println!("Hello {}!", name),
        &None => println!("I don't know who you are."),
    }
}

Speaking of matching, Rust 1.26 also supports matching on array slices, as in the following example:

fn foo(s: &[u8]) {
    match s {
        [1, x] => "Starts with one and has 2 elements",
        [a, b, c] => "Has three elements",
        _ => "Everything else",
    }
}

Two other relatively minor features in Rust 1.26 are the possibility of returning a Result from main and of defining inclusive ranges such as 1..=3.

If you are interested to know all that is new in Rust 1.26, do not miss the official release notes.

Rate this Article

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

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

Community comments

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