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

Implementors

The default timestamp precision is seconds.

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

Returns a Uri representing /

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

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.

The default match kind is MatchKind::Standard.

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

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 uses the Numerical representation.

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

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

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 is padded with zeroes.

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

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

Instantiate the default transformations, the identity transform.

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

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

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.

Creates a modifier that indicates the value uses the Long 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 value uses the Full representation.

Creates an empty iterator.

Creates an empty iterator.

This trait is implemented for tuples up to twelve items long.