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,
}

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

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

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.

enums

When using #[derive(Default)] on an enum, you need to choose which unit variant will be default. You do this by placing the #[default] attribute on the variant.

#[derive(Default)]
enum Kind {
    #[default]
    A,
    B,
    C,
}

You cannot use the #[default] attribute on non-unit or non-exhaustive variants.

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 }
}

Examples

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

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

Making your own:

enum Kind {
    A,
    B,
    C,
}

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

Implementations on Foreign Types

Constructs a new RandomState.

Creates a new empty cell.

Example
#![feature(once_cell)]

use std::lazy::SyncOnceCell;

fn main() {
    assert_eq!(SyncOnceCell::<()>::new(), SyncOnceCell::default());
}

Creates a new RwLock<T>, with the Default value for 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 Mutex<T>, with the Default value for T.

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

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

Creates an empty OsStr.

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

Constructs an empty OsString.

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 false

Returns the default value of 0

Returns the default value of 0

Creates a mutable empty slice.

Returns the default value of \x00

Returns the default value of 0

Creates an empty slice.

Creates an empty str

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.0

Creates an empty mutable str

Returns the default value of 0.0

Returns the default value of ()

Returns the default value of 0

Creates an empty Vec<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);

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

Creates an empty String.

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

Creates an empty BTreeMap.

Creates an empty BTreeSet.

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

Creates an empty CString.

Creates an empty BinaryHeap<T>.

Creates an empty LinkedList<T>.

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

Creates an empty deque.

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

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

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

Provides a default Hasher with fixed keys. This is typically used in conjunction with BuildHasherDefault to create [AHasher]s in order to hash the keys of the map.

Generally it is preferable to use [RandomState] instead, so that different hashmaps will have different keys. However if fixed keys are desireable this may be used instead.

Example

use std::hash::BuildHasherDefault;
use ahash::{AHasher, RandomState};
use std::collections::HashMap;

let mut map: HashMap<i32, i32, BuildHasherDefault<AHasher>> = HashMap::default();
map.insert(12, 34);

Constructs a new [AHasher] with fixed keys. If std is enabled these will be generated upon first invocation. Otherwise if the compile-time-rngfeature is enabled these will be generated at compile time. If neither of these features are available, hardcoded constants will be used.

Because the values are fixed, different hashers will all hash elements the same way. This could make hash values predictable, if DOS attacks are a concern. If this behaviour is not required, it may be preferable to use [RandomState] instead.

Examples
use ahash::AHasher;
use std::hash::Hasher;

let mut hasher_1 = AHasher::default();
let mut hasher_2 = AHasher::default();

hasher_1.write_u32(1234);
hasher_2.write_u32(1234);

assert_eq!(hasher_1.finish(), hasher_2.finish());

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

Return an empty IndexSet

Return an empty IndexMap

Implementors

Create an empty list.