core/default.rs
1//! The `Default` trait for types with a default value.
2
3#![stable(feature = "rust1", since = "1.0.0")]
4
5#[cfg(not(feature = "ferrocene_certified"))]
6use crate::ascii::Char as AsciiChar;
7
8/// A trait for giving a type a useful default value.
9///
10/// Sometimes, you want to fall back to some kind of default value, and
11/// don't particularly care what it is. This comes up often with `struct`s
12/// that define a set of options:
13///
14/// ```
15/// # #[allow(dead_code)]
16/// struct SomeOptions {
17/// foo: i32,
18/// bar: f32,
19/// }
20/// ```
21///
22/// How can we define some default values? You can use `Default`:
23///
24/// ```
25/// # #[allow(dead_code)]
26/// #[derive(Default)]
27/// struct SomeOptions {
28/// foo: i32,
29/// bar: f32,
30/// }
31///
32/// fn main() {
33/// let options: SomeOptions = Default::default();
34/// }
35/// ```
36///
37/// Now, you get all of the default values. Rust implements `Default` for various primitive types.
38///
39/// If you want to override a particular option, but still retain the other defaults:
40///
41/// ```
42/// # #[allow(dead_code)]
43/// # #[derive(Default)]
44/// # struct SomeOptions {
45/// # foo: i32,
46/// # bar: f32,
47/// # }
48/// fn main() {
49/// let options = SomeOptions { foo: 42, ..Default::default() };
50/// }
51/// ```
52///
53/// ## Derivable
54///
55/// This trait can be used with `#[derive]` if all of the type's fields implement
56/// `Default`. When `derive`d, it will use the default value for each field's type.
57///
58/// ### `enum`s
59///
60/// When using `#[derive(Default)]` on an `enum`, you need to choose which unit variant will be
61/// default. You do this by placing the `#[default]` attribute on the variant.
62///
63/// ```
64/// #[derive(Default)]
65/// enum Kind {
66/// #[default]
67/// A,
68/// B,
69/// C,
70/// }
71/// ```
72///
73/// You cannot use the `#[default]` attribute on non-unit or non-exhaustive variants.
74///
75/// The `#[default]` attribute was stabilized in Rust 1.62.0.
76///
77/// ## How can I implement `Default`?
78///
79/// Provide an implementation for the `default()` method that returns the value of
80/// your type that should be the default:
81///
82/// ```
83/// # #![allow(dead_code)]
84/// enum Kind {
85/// A,
86/// B,
87/// C,
88/// }
89///
90/// impl Default for Kind {
91/// fn default() -> Self { Kind::A }
92/// }
93/// ```
94///
95/// # Examples
96///
97/// ```
98/// # #[allow(dead_code)]
99/// #[derive(Default)]
100/// struct SomeOptions {
101/// foo: i32,
102/// bar: f32,
103/// }
104/// ```
105#[rustc_diagnostic_item = "Default"]
106#[stable(feature = "rust1", since = "1.0.0")]
107#[rustc_const_unstable(feature = "const_default", issue = "143894")]
108pub const trait Default: Sized {
109 /// Returns the "default value" for a type.
110 ///
111 /// Default values are often some kind of initial value, identity value, or anything else that
112 /// may make sense as a default.
113 ///
114 /// # Examples
115 ///
116 /// Using built-in default values:
117 ///
118 /// ```
119 /// let i: i8 = Default::default();
120 /// let (x, y): (Option<String>, f64) = Default::default();
121 /// let (a, b, (c, d)): (i32, u32, (bool, bool)) = Default::default();
122 /// ```
123 ///
124 /// Making your own:
125 ///
126 /// ```
127 /// # #[allow(dead_code)]
128 /// enum Kind {
129 /// A,
130 /// B,
131 /// C,
132 /// }
133 ///
134 /// impl Default for Kind {
135 /// fn default() -> Self { Kind::A }
136 /// }
137 /// ```
138 #[stable(feature = "rust1", since = "1.0.0")]
139 #[rustc_diagnostic_item = "default_fn"]
140 fn default() -> Self;
141}
142
143/// Derive macro generating an impl of the trait `Default`.
144#[rustc_builtin_macro(Default, attributes(default))]
145#[stable(feature = "builtin_macro_prelude", since = "1.38.0")]
146#[allow_internal_unstable(core_intrinsics)]
147#[cfg(not(feature = "ferrocene_certified"))]
148pub macro Default($item:item) {
149 /* compiler built-in */
150}
151
152macro_rules! default_impl {
153 ($t:ty, $v:expr, $doc:tt) => {
154 #[stable(feature = "rust1", since = "1.0.0")]
155 #[rustc_const_unstable(feature = "const_default", issue = "143894")]
156 impl const Default for $t {
157 #[inline(always)]
158 #[doc = $doc]
159 fn default() -> $t {
160 $v
161 }
162 }
163 };
164}
165
166#[cfg(not(feature = "ferrocene_certified"))]
167default_impl! { (), (), "Returns the default value of `()`" }
168#[cfg(not(feature = "ferrocene_certified"))]
169default_impl! { bool, false, "Returns the default value of `false`" }
170#[cfg(not(feature = "ferrocene_certified"))]
171default_impl! { char, '\x00', "Returns the default value of `\\x00`" }
172#[cfg(not(feature = "ferrocene_certified"))]
173default_impl! { AsciiChar, AsciiChar::Null, "Returns the default value of `Null`" }
174
175#[cfg(not(feature = "ferrocene_certified"))]
176default_impl! { usize, 0, "Returns the default value of `0`" }
177#[cfg(not(feature = "ferrocene_certified"))]
178default_impl! { u8, 0, "Returns the default value of `0`" }
179#[cfg(not(feature = "ferrocene_certified"))]
180default_impl! { u16, 0, "Returns the default value of `0`" }
181default_impl! { u32, 0, "Returns the default value of `0`" }
182#[cfg(not(feature = "ferrocene_certified"))]
183default_impl! { u64, 0, "Returns the default value of `0`" }
184#[cfg(not(feature = "ferrocene_certified"))]
185default_impl! { u128, 0, "Returns the default value of `0`" }
186
187#[cfg(not(feature = "ferrocene_certified"))]
188default_impl! { isize, 0, "Returns the default value of `0`" }
189#[cfg(not(feature = "ferrocene_certified"))]
190default_impl! { i8, 0, "Returns the default value of `0`" }
191#[cfg(not(feature = "ferrocene_certified"))]
192default_impl! { i16, 0, "Returns the default value of `0`" }
193#[cfg(not(feature = "ferrocene_certified"))]
194default_impl! { i32, 0, "Returns the default value of `0`" }
195#[cfg(not(feature = "ferrocene_certified"))]
196default_impl! { i64, 0, "Returns the default value of `0`" }
197#[cfg(not(feature = "ferrocene_certified"))]
198default_impl! { i128, 0, "Returns the default value of `0`" }
199
200#[cfg(not(feature = "ferrocene_certified"))]
201default_impl! { f16, 0.0f16, "Returns the default value of `0.0`" }
202#[cfg(not(feature = "ferrocene_certified"))]
203default_impl! { f32, 0.0f32, "Returns the default value of `0.0`" }
204#[cfg(not(feature = "ferrocene_certified"))]
205default_impl! { f64, 0.0f64, "Returns the default value of `0.0`" }
206#[cfg(not(feature = "ferrocene_certified"))]
207default_impl! { f128, 0.0f128, "Returns the default value of `0.0`" }