BT

Rust 1.19 Adds Untagged Unions and More

| by Sergio De Simone Follow 21 Followers on Jul 24, 2017. Estimated reading time: 1 minute |

Rust 1.19 introduces a number of language improvements, including non-tagged unions, and new standard library features.

Unions in Rust are similar to enums, but they are untagged. This implies that using unions in Rust is intrinsically unsafe, since no information is available to tell which variant of the union is effectively in use. This is how you can declare and use a Rust union:

union MyUnion {
    f1: u32,
    f2: f32,
}

let u = MyUnion { f1: 1 };
unsafe { u.f1 = 5 };
let value = unsafe { u.f1 };

fn f(u: MyUnion) {
    unsafe {
        match u {
            MyUnion { f1: 10 } => { println!("ten"); }
            MyUnion { f2 } => { println!("{}", f2); }
        }
    }
}

Besides making it easier for developers to interface with C APIs that expose unions, the introduction of unions in Rust also aims to simplify the implementation of space-efficient structures.

In Rust 1.19, a loop statement can now behave as an expression when used with break to return a value from it. E.g.:

let x = loop { break 7; };

On the library front, as mentioned, Rust 1.19 also includes a number of new features. Firstly, a couple of new macros, eprint! and eprintln!, make it straightforward to write to standard error instead of standard output.

Other new library features are the following:

Rust package manager, Cargo, also includes a few improvements. In particular, it no longer checks out a local working directory for the crates.io index, which contributes to improving performance and reduce the registry size. Additionally, build scripts can modify the environment they use to build a crate; the new --exclude option can be used to exclude specific packages when using --all.

If you already have Rust installed, you can update it by running:

rustup update stable

Otherwise, you can use rustup to build it for the first time.

Make sure to read the detailed release notes for more information.

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
BT