BT

InfoQ Homepage News Rust 1.19 Adds Untagged Unions and More

Rust 1.19 Adds Untagged Unions and More

Bookmarks

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

Is your profile up-to-date? Please take a moment to review and update.

Note: If updating/changing your email, a validation request will be sent

Company name:
Company role:
Company size:
Country/Zone:
State/Province/Region:
You will be sent an email to validate the new email address. This pop-up will close itself in a few moments.