BT

Rust Has Got Existential Types

| by Sergio De Simone Follow 18 Followers on May 24, 2018. Estimated reading time: 2 minutes |

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

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

Discuss

Login to InfoQ to interact with what matters most to you.


Recover your password...

Follow

Follow your favorite topics and editors

Quick overview of most important highlights in the industry and on the site.

Like

More signal, less noise

Build your own feed by choosing topics you want to read about and editors you want to hear from.

Notifications

Stay up-to-date

Set up your notifications and don't miss out on content that matters to you

BT