]> git.lizzy.rs Git - rust.git/blob - src/libcore/default.rs
Auto merge of #22517 - brson:relnotes, r=Gankro
[rust.git] / src / libcore / default.rs
1 // Copyright 2013 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 //! The `Default` trait for types which may have meaningful default values.
12 //!
13 //! Sometimes, you want to fall back to some kind of default value, and
14 //! don't particularly care what it is. This comes up often with `struct`s
15 //! that define a set of options:
16 //!
17 //! ```
18 //! struct SomeOptions {
19 //!     foo: int,
20 //!     bar: f32,
21 //! }
22 //! ```
23 //!
24 //! How can we define some default values? You can use `Default`:
25 //!
26 //! ```
27 //! use std::default::Default;
28 //!
29 //! #[derive(Default)]
30 //! struct SomeOptions {
31 //!     foo: int,
32 //!     bar: f32,
33 //! }
34 //!
35 //!
36 //! fn main() {
37 //!     let options: SomeOptions = Default::default();
38 //! }
39 //! ```
40 //!
41 //! Now, you get all of the default values. Rust implements `Default` for various primitives types.
42 //! If you have your own type, you need to implement `Default` yourself:
43 //!
44 //! ```
45 //! use std::default::Default;
46 //!
47 //! enum Kind {
48 //!     A,
49 //!     B,
50 //!     C,
51 //! }
52 //!
53 //! impl Default for Kind {
54 //!     fn default() -> Kind { Kind::A }
55 //! }
56 //!
57 //! #[derive(Default)]
58 //! struct SomeOptions {
59 //!     foo: int,
60 //!     bar: f32,
61 //!     baz: Kind,
62 //! }
63 //!
64 //!
65 //! fn main() {
66 //!     let options: SomeOptions = Default::default();
67 //! }
68 //! ```
69 //!
70 //! If you want to override a particular option, but still retain the other defaults:
71 //!
72 //! ```
73 //! # use std::default::Default;
74 //! # #[derive(Default)]
75 //! # struct SomeOptions {
76 //! #     foo: int,
77 //! #     bar: f32,
78 //! # }
79 //! fn main() {
80 //!     let options = SomeOptions { foo: 42, ..Default::default() };
81 //! }
82 //! ```
83
84 #![stable(feature = "rust1", since = "1.0.0")]
85
86 /// A trait that types which have a useful default value should implement.
87 ///
88 /// A struct can derive default implementations of `Default` for basic types using
89 /// `#[derive(Default)]`.
90 ///
91 /// # Examples
92 ///
93 /// ```
94 /// #[derive(Default)]
95 /// struct SomeOptions {
96 ///     foo: int,
97 ///     bar: f32,
98 /// }
99 /// ```
100 #[stable(feature = "rust1", since = "1.0.0")]
101 pub trait Default {
102     /// Returns the "default value" for a type.
103     ///
104     /// Default values are often some kind of initial value, identity value, or anything else that
105     /// may make sense as a default.
106     ///
107     /// # Examples
108     ///
109     /// Using built-in default values:
110     ///
111     /// ```
112     /// use std::default::Default;
113     ///
114     /// let i: i8 = Default::default();
115     /// let (x, y): (Option<String>, f64) = Default::default();
116     /// let (a, b, (c, d)): (int, uint, (bool, bool)) = Default::default();
117     /// ```
118     ///
119     /// Making your own:
120     ///
121     /// ```
122     /// use std::default::Default;
123     ///
124     /// enum Kind {
125     ///     A,
126     ///     B,
127     ///     C,
128     /// }
129     ///
130     /// impl Default for Kind {
131     ///     fn default() -> Kind { Kind::A }
132     /// }
133     /// ```
134     #[stable(feature = "rust1", since = "1.0.0")]
135     fn default() -> Self;
136 }
137
138 macro_rules! default_impl {
139     ($t:ty, $v:expr) => {
140         #[stable(feature = "rust1", since = "1.0.0")]
141         impl Default for $t {
142             #[inline]
143             #[stable(feature = "rust1", since = "1.0.0")]
144             fn default() -> $t { $v }
145         }
146     }
147 }
148
149 default_impl! { (), () }
150 default_impl! { bool, false }
151 default_impl! { char, '\x00' }
152
153 default_impl! { uint, 0 }
154 default_impl! { u8, 0 }
155 default_impl! { u16, 0 }
156 default_impl! { u32, 0 }
157 default_impl! { u64, 0 }
158
159 default_impl! { int, 0 }
160 default_impl! { i8, 0 }
161 default_impl! { i16, 0 }
162 default_impl! { i32, 0 }
163 default_impl! { i64, 0 }
164
165 default_impl! { f32, 0.0f32 }
166 default_impl! { f64, 0.0f64 }
167