Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ


Choose your language

InfoQ Homepage News Rust 1.20 Brings Associated Constants and More

Rust 1.20 Brings Associated Constants and More

This item in japanese


Rust 1.20 adds type-associated constants, a number of library stabilization, and improved credential hiding in Cargo.

Rust 1.20 allows now to define constants associated to traits, structs, and enums. For example, the following snippet defines an ID constant that is not associated to any particular instance and can be used as Struct::ID:

struct Struct;

impl Struct {
    const ID: u32 = 0;

When defining an associated constant for a trait, you are not required to define a value for it, letting the concrete trait implementation do it:

trait Trait {
    const ID: u32;

struct Struct;

impl Trait for Struct {
    const ID: u32 = 5;

Additionally, Rust 1.20 fixes a number of bugs that affected macros, specifically by attempting to predict whether a token can be matched against a fragment specifier. The Rust compiler has also been improved by improving type coercion within structs, enabling the LLVM WebAssembly backend, improving error messages, and more.

On the standard library front, Rust 1.20 introduced a number of improvements and stabilizations:

  • The unimplemented! macro allows now to specify an informative message.
  • min and max have been rewritten in Rust.
  • Runtime guarantees have been improved by providing stack probes, which make sure a stack overflow will trigger a segfault instead of being accidentally ignored; and better integration with Linux stack guard mechanism, which were introduced to fix a vulnerability in the kernel.
  • Unstable sort functions are now available through slice::sort_unstable_by_key, slice::sort_unstable_by, and slice::sort_unstable. Unstable sorting does not guarantee that equally-ranking items will always appear in the same order when they are sorted multiple times, which makes it usually faster and easier to code.

Finally, Rust package manager Cargo now stores the authentication token in a separate file, named ~/.cargo/credentials, which can be properly protected from other users attempting to access it.

To get the full detail of what is new in Rust 1.20, do not forget to read the release notes.

Rate this Article