W3cubDocs

/Rust

Function std::default::default

pub fn default<T>() -> T where    T: Default, 
🔬 This is a nightly-only experimental API. (default_free_fn #73014)

Return the default value of a type according to the Default trait.

The type to return is inferred from context; this is equivalent to Default::default() but shorter to type.

For example:

#![feature(default_free_fn)]

use std::default::default;

#[derive(Default)]
struct AppConfig {
    foo: FooConfig,
    bar: BarConfig,
}

#[derive(Default)]
struct FooConfig {
    foo: i32,
}

#[derive(Default)]
struct BarConfig {
    bar: f32,
    baz: u8,
}

fn main() {
    let options = AppConfig {
        foo: default(),
        bar: BarConfig {
            bar: 10.1,
            ..default()
        },
    };
}

© 2010 The Rust Project Developers
Licensed under the Apache License, Version 2.0 or the MIT license, at your option.
https://doc.rust-lang.org/std/default/fn.default.html