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.

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

Creates a new empty cell.

Example
#![feature(once_cell)]

use std::lazy::SyncOnceCell;

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

Constructs an empty OsString.

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

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

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

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

Creates an empty CString.

Returns the default value of 0

Returns the default value of false

Returns the default value of 0

Returns the default value of 0

Returns the default value of 0

Returns the default value of ()

Creates an empty str

Creates an empty mutable str

Returns the default value of 0

Returns the default value of \x00

Returns the default value of 0.0

Returns the default value of 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

Creates a mutable empty slice.

Returns the default value of 0

Returns the default value of 0

Creates an empty slice.

Returns the default value of 0

Creates an empty LinkedList<T>.

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 Vec<T>.

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 a Box<T>, with the Default value for T.

Creates an empty String.

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 BinaryHeap<T>.

The default match kind is MatchKind::Standard.

Creates a modifier that indicates that the value uses the Iso representation.

Creates a modifier that indicates the value uses the Long representation.

Creates a modifier that indicates the value is padded with zeroes.

Creates a modifier that indicates the value is padded with zeroes.

Creates a modifier that indicates the value is padded with zeroes and has the 24-hour representation.

Creates an instance of this type that indicates the value uses the Numerical representation, is padded with zeroes, and is case-sensitive when parsing.

Creates a modifier that indicates the value is padded with zeroes.

Creates a modifier that indicates the value is padded with zeroes.

Creates a modifier that indicates the stringified value contains one or more digits.

Creates a modifier that indicates that the value is padded with zeroes and uses the Iso representation.

Creates a modifier that indicates the value uses the Full representation, is padded with zeroes, uses the Gregorian calendar as its base, and only includes the year’s sign if necessary.

Creates a modifier that indicates the stringified value contains one or more digits.

Creates a modifier that indicates the value is padded with zeroes.

Creates a modifier that indicates the value is padded with zeroes.

Creates a modifier that indicates the value uses the + sign for all positive values and is padded with zeroes.

Creates a modifier that indicates the value uses the Full representation.

Creates a modifier that indicates the value is padded with zeroes.

Creates a modifier that indicates the value uses the Numerical representation.

Creates a modifier that indicates the value uses the upper-case representation and is case-sensitive when parsing.

Creates a modifier that indicates the value uses the Long representation and is case-sensitive when parsing. If the representation is changed to a numerical one, the instance defaults to one-based indexing.

Returns a style with no properties set. Formatting text using this style returns the exact same text.

use ansi_term::Style;
assert_eq!(None,  Style::default().foreground);
assert_eq!(None,  Style::default().background);
assert_eq!(false, Style::default().is_bold);
assert_eq!("txt", Style::default().paint("txt").to_string());

Initialize the compressor with a level of 4, zlib wrapper and the default strategy.

Create a new tinfl_decompressor with all fields set to 0.

Create a default empty GlobSet.

Choose the default compression, a balance between speed and size.

The defaults are that of https://url.spec.whatwg.org/#idna

Returns a pixel density with a pixel aspect ratio of 1

Instantiate the default transformations, the identity transform.

Create a new tinfl_decompressor with all fields set to 0.

Initialize the compressor with a level of 4, zlib wrapper and the default strategy.

Returns the options describing the default compression level.

Creates an empty iterator.

Creates an empty iterator.

Returns a Uri representing /

Create an empty Full.

Returns [MissedTickBehavior::Burst].

For most usecases, the Burst strategy is what is desired. Additionally, to preserve backwards compatibility, the Burst strategy must be the default. For these reasons, [MissedTickBehavior::Burst] is the default for [MissedTickBehavior]. See Burst for more details.

Returns Body::empty().

Returns the current default dispatcher

Return an empty IndexSet

Return an empty IndexMap

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

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

Implementors

The default value is Value::Null.

This is useful for handling omitted Value fields when deserializing.

Examples

use serde_json::Value;

#[derive(Deserialize)]
struct Settings {
    level: i32,
    #[serde(default)]
    extras: Value,
}

let data = r#" { "level": 42 } "#;
let s: Settings = serde_json::from_str(data)?;

assert_eq!(s.level, 42);
assert_eq!(s.extras, Value::Null);

The default timestamp precision is seconds.