2022-04-30 12:01:31 -05:00
|
|
|
//! The `Default` trait for types with a default value.
|
2013-08-10 08:38:00 -05:00
|
|
|
|
2015-01-23 23:48:20 -06:00
|
|
|
#![stable(feature = "rust1", since = "1.0.0")]
|
2014-07-18 18:01:55 -05:00
|
|
|
|
2016-05-23 11:52:38 -05:00
|
|
|
/// A trait for giving a type a useful default value.
|
|
|
|
///
|
2016-05-23 12:47:28 -05:00
|
|
|
/// 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 `struct`s
|
|
|
|
/// that define a set of options:
|
2014-09-22 15:43:47 -05:00
|
|
|
///
|
2016-05-23 12:47:28 -05:00
|
|
|
/// ```
|
|
|
|
/// # #[allow(dead_code)]
|
|
|
|
/// struct SomeOptions {
|
|
|
|
/// foo: i32,
|
|
|
|
/// bar: f32,
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// How can we define some default values? You can use `Default`:
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// # #[allow(dead_code)]
|
|
|
|
/// #[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:
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// # #[allow(dead_code)]
|
|
|
|
/// # #[derive(Default)]
|
|
|
|
/// # struct SomeOptions {
|
|
|
|
/// # foo: i32,
|
|
|
|
/// # bar: f32,
|
|
|
|
/// # }
|
|
|
|
/// fn main() {
|
|
|
|
/// let options = SomeOptions { foo: 42, ..Default::default() };
|
|
|
|
/// }
|
|
|
|
/// ```
|
2016-05-22 18:24:58 -05:00
|
|
|
///
|
|
|
|
/// ## Derivable
|
|
|
|
///
|
|
|
|
/// This trait can be used with `#[derive]` if all of the type's fields implement
|
|
|
|
/// `Default`. When `derive`d, it will use the default value for each field's type.
|
|
|
|
///
|
2022-03-08 14:44:52 -06:00
|
|
|
/// ### `enum`s
|
|
|
|
///
|
|
|
|
/// 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.
|
|
|
|
///
|
2016-05-22 18:24:58 -05:00
|
|
|
/// ## How can I implement `Default`?
|
|
|
|
///
|
2020-04-16 16:30:53 -05:00
|
|
|
/// Provide an implementation for the `default()` method that returns the value of
|
2016-05-22 18:24:58 -05:00
|
|
|
/// your type that should be the default:
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// # #![allow(dead_code)]
|
|
|
|
/// enum Kind {
|
|
|
|
/// A,
|
|
|
|
/// B,
|
|
|
|
/// C,
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// impl Default for Kind {
|
2018-11-01 05:52:44 -05:00
|
|
|
/// fn default() -> Self { Kind::A }
|
2016-05-22 18:24:58 -05:00
|
|
|
/// }
|
|
|
|
/// ```
|
2014-09-22 15:43:47 -05:00
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```
|
2015-11-03 09:27:03 -06:00
|
|
|
/// # #[allow(dead_code)]
|
2015-01-03 21:54:18 -06:00
|
|
|
/// #[derive(Default)]
|
2014-09-22 15:43:47 -05:00
|
|
|
/// struct SomeOptions {
|
2015-02-18 07:41:13 -06:00
|
|
|
/// foo: i32,
|
2014-09-22 15:43:47 -05:00
|
|
|
/// bar: f32,
|
|
|
|
/// }
|
|
|
|
/// ```
|
2021-03-04 12:09:02 -06:00
|
|
|
#[cfg_attr(not(test), rustc_diagnostic_item = "Default")]
|
2015-01-23 23:48:20 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2022-11-01 07:45:58 -05:00
|
|
|
#[const_trait]
|
2015-10-23 20:51:38 -05:00
|
|
|
pub trait Default: Sized {
|
2014-09-22 15:43:47 -05:00
|
|
|
/// Returns the "default value" for a type.
|
2014-07-19 05:10:09 -05:00
|
|
|
///
|
2014-09-22 15:43:47 -05:00
|
|
|
/// 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:
|
2014-07-19 05:10:09 -05:00
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// let i: i8 = Default::default();
|
|
|
|
/// let (x, y): (Option<String>, f64) = Default::default();
|
2015-02-18 07:41:13 -06:00
|
|
|
/// let (a, b, (c, d)): (i32, u32, (bool, bool)) = Default::default();
|
2014-07-19 05:10:09 -05:00
|
|
|
/// ```
|
2014-09-22 15:43:47 -05:00
|
|
|
///
|
|
|
|
/// Making your own:
|
|
|
|
///
|
|
|
|
/// ```
|
2015-11-03 09:27:03 -06:00
|
|
|
/// # #[allow(dead_code)]
|
2014-09-22 15:43:47 -05:00
|
|
|
/// enum Kind {
|
|
|
|
/// A,
|
|
|
|
/// B,
|
|
|
|
/// C,
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// impl Default for Kind {
|
2018-11-01 05:52:44 -05:00
|
|
|
/// fn default() -> Self { Kind::A }
|
2014-09-22 15:43:47 -05:00
|
|
|
/// }
|
|
|
|
/// ```
|
2015-01-23 23:48:20 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2013-08-10 08:38:00 -05:00
|
|
|
fn default() -> Self;
|
|
|
|
}
|
2013-09-11 23:49:25 -05:00
|
|
|
|
2020-06-03 22:36:53 -05:00
|
|
|
/// 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()
|
|
|
|
/// },
|
|
|
|
/// };
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
#[unstable(feature = "default_free_fn", issue = "73014")]
|
2021-10-14 17:54:55 -05:00
|
|
|
#[must_use]
|
2020-06-03 22:36:53 -05:00
|
|
|
#[inline]
|
|
|
|
pub fn default<T: Default>() -> T {
|
|
|
|
Default::default()
|
|
|
|
}
|
|
|
|
|
2019-07-27 17:51:21 -05:00
|
|
|
/// Derive macro generating an impl of the trait `Default`.
|
2021-07-30 07:46:56 -05:00
|
|
|
#[rustc_builtin_macro(Default, attributes(default))]
|
2019-07-27 17:51:21 -05:00
|
|
|
#[stable(feature = "builtin_macro_prelude", since = "1.38.0")]
|
|
|
|
#[allow_internal_unstable(core_intrinsics)]
|
|
|
|
pub macro Default($item:item) {
|
|
|
|
/* compiler built-in */
|
|
|
|
}
|
|
|
|
|
2014-11-14 11:18:10 -06:00
|
|
|
macro_rules! default_impl {
|
2017-07-17 12:49:40 -05:00
|
|
|
($t:ty, $v:expr, $doc:tt) => {
|
2015-01-23 23:48:20 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2021-08-14 11:35:12 -05:00
|
|
|
#[rustc_const_unstable(feature = "const_default_impls", issue = "87864")]
|
|
|
|
impl const Default for $t {
|
2014-05-26 12:33:04 -05:00
|
|
|
#[inline]
|
2017-07-15 08:35:03 -05:00
|
|
|
#[doc = $doc]
|
2021-01-09 11:00:45 -06:00
|
|
|
fn default() -> $t {
|
|
|
|
$v
|
|
|
|
}
|
2014-05-26 12:33:04 -05:00
|
|
|
}
|
2021-01-09 11:00:45 -06:00
|
|
|
};
|
2014-11-14 11:18:10 -06:00
|
|
|
}
|
|
|
|
|
2017-07-15 10:34:37 -05:00
|
|
|
default_impl! { (), (), "Returns the default value of `()`" }
|
|
|
|
default_impl! { bool, false, "Returns the default value of `false`" }
|
|
|
|
default_impl! { char, '\x00', "Returns the default value of `\\x00`" }
|
2014-05-26 12:33:04 -05:00
|
|
|
|
2017-07-15 10:34:37 -05:00
|
|
|
default_impl! { usize, 0, "Returns the default value of `0`" }
|
|
|
|
default_impl! { u8, 0, "Returns the default value of `0`" }
|
|
|
|
default_impl! { u16, 0, "Returns the default value of `0`" }
|
|
|
|
default_impl! { u32, 0, "Returns the default value of `0`" }
|
|
|
|
default_impl! { u64, 0, "Returns the default value of `0`" }
|
|
|
|
default_impl! { u128, 0, "Returns the default value of `0`" }
|
2014-05-26 12:33:04 -05:00
|
|
|
|
2017-07-15 10:34:37 -05:00
|
|
|
default_impl! { isize, 0, "Returns the default value of `0`" }
|
|
|
|
default_impl! { i8, 0, "Returns the default value of `0`" }
|
|
|
|
default_impl! { i16, 0, "Returns the default value of `0`" }
|
|
|
|
default_impl! { i32, 0, "Returns the default value of `0`" }
|
|
|
|
default_impl! { i64, 0, "Returns the default value of `0`" }
|
|
|
|
default_impl! { i128, 0, "Returns the default value of `0`" }
|
2014-05-26 12:33:04 -05:00
|
|
|
|
2017-07-15 10:34:37 -05:00
|
|
|
default_impl! { f32, 0.0f32, "Returns the default value of `0.0`" }
|
|
|
|
default_impl! { f64, 0.0f64, "Returns the default value of `0.0`" }
|