Associated constants
You can define traits, structs, and enums that have “associated functions”:
struct Struct;
impl Struct {
fn foo() {
println!("foo is an associated function of Struct");
}
}
fn main() {
Struct::foo();
}
These are called “associated functions” because they are functions that areassociated with the type, that is, they’re attached to the type itself, andnot any particular instance.
Rust 1.20 adds the ability to define “associated constants” as well:
struct Struct;
impl Struct {
const ID: u32 = 0;
}
fn main() {
println!("the ID of Struct is: {}", Struct::ID);
}
That is, the constant ID
is associated with Struct
. Like functions,associated constants work with traits and enums as well.
Traits have an extra ability with associated constants that gives them someextra power. With a trait, you can use an associated constant in the same wayyou’d use an associated type: by declaring it, but not giving it a value. Theimplementor of the trait then declares its value upon implementation:
trait Trait {
const ID: u32;
}
struct Struct;
impl Trait for Struct {
const ID: u32 = 5;
}
fn main() {
println!("{}", Struct::ID);
}
Before this feature, if you wanted to make a trait that represented floatingpoint numbers, you’d have to write this:
#![allow(unused_variables)]
fn main() {
trait Float {
fn nan() -> Self;
fn infinity() -> Self;
// ...
}
}
This is slightly unwieldy, but more importantly, because they’re functions,they cannot be used in constant expressions, even though they only return aconstant. Because of this, a design for Float
would also have to includeconstants as well:
mod f32 {
const NAN: f32 = 0.0f32 / 0.0f32;
const INFINITY: f32 = 1.0f32 / 0.0f32;
impl Float for f32 {
fn nan() -> Self {
f32::NAN
}
fn infinity() -> Self {
f32::INFINITY
}
}
}
Associated constants let you do this in a much cleaner way. This traitdefinition:
#![allow(unused_variables)]
fn main() {
trait Float {
const NAN: Self;
const INFINITY: Self;
// ...
}
}
Leads to this implementation:
mod f32 {
impl Float for f32 {
const NAN: f32 = 0.0f32 / 0.0f32;
const INFINITY: f32 = 1.0f32 / 0.0f32;
}
}
much cleaner, and more versatile.