Trait std::default::Default1.0.0[][src]

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

A trait for giving a type a useful default value.

Sometimes, you want to fall back to some kind of default value, and don’t particularly care what it is. This comes up often with structs that define a set of options:

struct SomeOptions {
    foo: i32,
    bar: f32,
}
Run

How can we define some default values? You can use Default:

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

fn main() {
    let options: SomeOptions = Default::default();
}
Run

Now, you get all of the default values. Rust implements Default for various primitives types.

If you want to override a particular option, but still retain the other defaults:

fn main() {
    let options = SomeOptions { foo: 42, ..Default::default() };
}
Run

Derivable

This trait can be used with #[derive] if all of the type’s fields implement Default. When derived, it will use the default value for each field’s type.

How can I implement Default?

Provide an implementation for the default() method that returns the value of your type that should be the default:

enum Kind {
    A,
    B,
    C,
}

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

Examples

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

Required methods

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();
Run

Making your own:

enum Kind {
    A,
    B,
    C,
}

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

Implementors

Creates an empty OsStr.

Returns the default value of false

Returns the default value of \x00

Returns the default value of 0.0

Returns the default value of 0.0

Returns the default value of 0

Returns the default value of 0

Returns the default value of 0

Returns the default value of 0

Returns the default value of 0

Returns the default value of 0

Returns the default value of 0

Returns the default value of 0

Returns the default value of 0

Returns the default value of 0

Returns the default value of 0

Returns the default value of ()

Returns the default value of 0

Creates a new DefaultHasher using new. See its documentation for more.

Constructs a new RandomState.

Creates an empty CString.

Constructs an empty OsString.

Creates an empty String.

Creates an AtomicBool initialized to false.

Creates a Condvar which is ready to be waited on and notified.

Creates an empty str

Creates an empty mutable str

Creates an owned Cow<’a, B> with the default value for the contained owned value.

Creates an empty slice.

Creates a mutable empty slice.

Creates an empty BTreeMap.

Creates an empty HashMap<K, V, S>, with the Default value for the hasher.

Returns None.

Examples

let opt: Option<u32> = Option::default();
assert!(opt.is_none());
Run

Creates a Box<T>, with the Default value for T.

Creates a Cell<T>, with the Default value for T.

Creates a RefCell<T>, with the Default value for T.

Creates an UnsafeCell, with the Default value for T.

Creates an empty BTreeSet.

Creates an empty BinaryHeap<T>.

Creates an empty LinkedList<T>.

Creates an empty VecDeque<T>.

Creates a new lazy value using Default as the initializing function.

Creates a new Rc<T>, with the Default value for T.

Examples

use std::rc::Rc;

let x: Rc<i32> = Default::default();
assert_eq!(*x, 0);
Run

Constructs a new Weak<T>, without allocating any memory. Calling upgrade on the return value always gives None.

Examples

use std::rc::Weak;

let empty: Weak<i64> = Default::default();
assert!(empty.upgrade().is_none());
Run

Creates a null AtomicPtr<T>.

Creates a new Arc<T>, with the Default value for T.

Examples

use std::sync::Arc;

let x: Arc<i32> = Default::default();
assert_eq!(*x, 0);
Run

Constructs a new Weak<T>, without allocating memory. Calling upgrade on the return value always gives None.

Examples

use std::sync::Weak;

let empty: Weak<i64> = Default::default();
assert!(empty.upgrade().is_none());
Run

Creates an empty Vec<T>.

Creates an empty HashSet<T, S> with the Default value for the hasher.

Creates a new lazy value using Default as the initializing function.

Creates a new RwLock<T>, with the Default value for T.

Creates a Mutex<T>, with the Default value for T.