Trait std::default::Default [] [src]

pub trait Default {
    fn default() -> Self;
}

A trait for giving a type a useful default value.

A struct can derive default implementations of Default for basic types using #[derive(Default)].

Examples

#[derive(Default)]
struct SomeOptions {
    foo: i32,
    bar: f32,
}

Required Methods

fn default() -> Self

Returns the "default value" for a type.

Default values are often some kind of initial value, identity value, or anything else that may make sense as a default.

Examples

Using built-in default values:

let i: i8 = Default::default();
let (x, y): (Option<String>, f64) = Default::default();
let (a, b, (c, d)): (i32, u32, (bool, bool)) = Default::default();

Making your own:

enum Kind {
    A,
    B,
    C,
}

impl Default for Kind {
    fn default() -> Kind { Kind::A }
}

Implementors