8
8
// option. This file may not be copied, modified, or distributed
9
9
// except according to those terms.
10
10
11
- //! The `Default` trait for types which may have meaningful default values
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
+ //! #[deriving(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 { A }
55
+ //! }
56
+ //!
57
+ //! #[deriving(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
+ //! # #[deriving(Default)]
75
+ //! # struct SomeOptions {
76
+ //! # foo: int,
77
+ //! # bar: f32,
78
+ //! # }
79
+ //! fn main() {
80
+ //! let options = SomeOptions { foo: 42, ..Default::default() };
81
+ //! }
82
+ //! ```
12
83
13
84
#![ stable]
14
85
15
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
+ /// `#[deriving(Default)]`.
90
+ ///
91
+ /// # Examples
92
+ ///
93
+ /// ```
94
+ /// #[deriving(Default)]
95
+ /// struct SomeOptions {
96
+ /// foo: int,
97
+ /// bar: f32,
98
+ /// }
99
+ /// ```
16
100
pub trait Default {
17
- /// Return the "default value" for a type.
101
+ /// Returns the "default value" for a type.
18
102
///
19
- /// # Example
103
+ /// Default values are often some kind of initial value, identity value, or anything else that
104
+ /// may make sense as a default.
105
+ ///
106
+ /// # Examples
107
+ ///
108
+ /// Using built-in default values:
20
109
///
21
110
/// ```
22
111
/// use std::default::Default;
@@ -25,6 +114,22 @@ pub trait Default {
25
114
/// let (x, y): (Option<String>, f64) = Default::default();
26
115
/// let (a, b, (c, d)): (int, uint, (bool, bool)) = Default::default();
27
116
/// ```
117
+ ///
118
+ /// Making your own:
119
+ ///
120
+ /// ```
121
+ /// use std::default::Default;
122
+ ///
123
+ /// enum Kind {
124
+ /// A,
125
+ /// B,
126
+ /// C,
127
+ /// }
128
+ ///
129
+ /// impl Default for Kind {
130
+ /// fn default() -> Kind { A }
131
+ /// }
132
+ /// ```
28
133
fn default ( ) -> Self ;
29
134
}
30
135
0 commit comments