1 //! The `Default` trait for types which may have meaningful default values.
3 #![stable(feature = "rust1", since = "1.0.0")]
5 /// A trait for giving a type a useful default value.
7 /// Sometimes, you want to fall back to some kind of default value, and
8 /// don't particularly care what it is. This comes up often with `struct`s
9 /// that define a set of options:
12 /// # #[allow(dead_code)]
13 /// struct SomeOptions {
19 /// How can we define some default values? You can use `Default`:
22 /// # #[allow(dead_code)]
23 /// #[derive(Default)]
24 /// struct SomeOptions {
30 /// let options: SomeOptions = Default::default();
34 /// Now, you get all of the default values. Rust implements `Default` for various primitives types.
36 /// If you want to override a particular option, but still retain the other defaults:
39 /// # #[allow(dead_code)]
40 /// # #[derive(Default)]
41 /// # struct SomeOptions {
46 /// let options = SomeOptions { foo: 42, ..Default::default() };
52 /// This trait can be used with `#[derive]` if all of the type's fields implement
53 /// `Default`. When `derive`d, it will use the default value for each field's type.
55 /// ## How can I implement `Default`?
57 /// Provides an implementation for the `default()` method that returns the value of
58 /// your type that should be the default:
61 /// # #![allow(dead_code)]
68 /// impl Default for Kind {
69 /// fn default() -> Self { Kind::A }
76 /// # #[allow(dead_code)]
77 /// #[derive(Default)]
78 /// struct SomeOptions {
83 #[stable(feature = "rust1", since = "1.0.0")]
84 pub trait Default: Sized {
85 /// Returns the "default value" for a type.
87 /// Default values are often some kind of initial value, identity value, or anything else that
88 /// may make sense as a default.
92 /// Using built-in default values:
95 /// let i: i8 = Default::default();
96 /// let (x, y): (Option<String>, f64) = Default::default();
97 /// let (a, b, (c, d)): (i32, u32, (bool, bool)) = Default::default();
103 /// # #[allow(dead_code)]
110 /// impl Default for Kind {
111 /// fn default() -> Self { Kind::A }
114 #[stable(feature = "rust1", since = "1.0.0")]
115 fn default() -> Self;
118 /// Derive macro generating an impl of the trait `Default`.
119 #[rustc_builtin_macro]
120 #[stable(feature = "builtin_macro_prelude", since = "1.38.0")]
121 #[allow_internal_unstable(core_intrinsics)]
122 pub macro Default($item:item) { /* compiler built-in */ }
124 macro_rules! default_impl {
125 ($t:ty, $v:expr, $doc:tt) => {
126 #[stable(feature = "rust1", since = "1.0.0")]
127 impl Default for $t {
130 fn default() -> $t { $v }
135 default_impl! { (), (), "Returns the default value of `()`" }
136 default_impl! { bool, false, "Returns the default value of `false`" }
137 default_impl! { char, '\x00', "Returns the default value of `\\x00`" }
139 default_impl! { usize, 0, "Returns the default value of `0`" }
140 default_impl! { u8, 0, "Returns the default value of `0`" }
141 default_impl! { u16, 0, "Returns the default value of `0`" }
142 default_impl! { u32, 0, "Returns the default value of `0`" }
143 default_impl! { u64, 0, "Returns the default value of `0`" }
144 default_impl! { u128, 0, "Returns the default value of `0`" }
146 default_impl! { isize, 0, "Returns the default value of `0`" }
147 default_impl! { i8, 0, "Returns the default value of `0`" }
148 default_impl! { i16, 0, "Returns the default value of `0`" }
149 default_impl! { i32, 0, "Returns the default value of `0`" }
150 default_impl! { i64, 0, "Returns the default value of `0`" }
151 default_impl! { i128, 0, "Returns the default value of `0`" }
153 default_impl! { f32, 0.0f32, "Returns the default value of `0.0`" }
154 default_impl! { f64, 0.0f64, "Returns the default value of `0.0`" }