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

fn main() { #[allow(dead_code)] #[derive(Default)] struct SomeOptions { foo: i32, bar: f32, } }
#[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:

fn main() { let i: i8 = Default::default(); let (x, y): (Option<String>, f64) = Default::default(); let (a, b, (c, d)): (i32, u32, (bool, bool)) = Default::default(); }
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:

fn main() { #[allow(dead_code)] enum Kind { A, B, C, } impl Default for Kind { fn default() -> Kind { Kind::A } } }
enum Kind {
    A,
    B,
    C,
}

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

Implementors