BT

Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ

Topics

Choose your language

InfoQ Homepage News Rust 1.19 Adds Untagged Unions and More

Rust 1.19 Adds Untagged Unions and More

This item in japanese

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

BT