|
9 | 9 | // except according to those terms. |
10 | 10 |
|
11 | 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 | | -//! # #[allow(dead_code)] |
19 | | -//! struct SomeOptions { |
20 | | -//! foo: i32, |
21 | | -//! bar: f32, |
22 | | -//! } |
23 | | -//! ``` |
24 | | -//! |
25 | | -//! How can we define some default values? You can use `Default`: |
26 | | -//! |
27 | | -//! ``` |
28 | | -//! # #[allow(dead_code)] |
29 | | -//! #[derive(Default)] |
30 | | -//! struct SomeOptions { |
31 | | -//! foo: i32, |
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 | | -//! # #![allow(dead_code)] |
46 | | -//! enum Kind { |
47 | | -//! A, |
48 | | -//! B, |
49 | | -//! C, |
50 | | -//! } |
51 | | -//! |
52 | | -//! impl Default for Kind { |
53 | | -//! fn default() -> Kind { Kind::A } |
54 | | -//! } |
55 | | -//! |
56 | | -//! #[derive(Default)] |
57 | | -//! struct SomeOptions { |
58 | | -//! foo: i32, |
59 | | -//! bar: f32, |
60 | | -//! baz: Kind, |
61 | | -//! } |
62 | | -//! |
63 | | -//! |
64 | | -//! fn main() { |
65 | | -//! let options: SomeOptions = Default::default(); |
66 | | -//! } |
67 | | -//! ``` |
68 | | -//! |
69 | | -//! If you want to override a particular option, but still retain the other defaults: |
70 | | -//! |
71 | | -//! ``` |
72 | | -//! # #[allow(dead_code)] |
73 | | -//! # #[derive(Default)] |
74 | | -//! # struct SomeOptions { |
75 | | -//! # foo: i32, |
76 | | -//! # bar: f32, |
77 | | -//! # } |
78 | | -//! fn main() { |
79 | | -//! let options = SomeOptions { foo: 42, ..Default::default() }; |
80 | | -//! } |
81 | | -//! ``` |
82 | 12 |
|
83 | 13 | #![stable(feature = "rust1", since = "1.0.0")] |
84 | 14 |
|
85 | 15 | use marker::Sized; |
86 | 16 |
|
87 | 17 | /// A trait for giving a type a useful default value. |
88 | 18 | /// |
89 | | -/// For more information, see |
90 | | -/// [the module-level documentation][module]. |
| 19 | +/// Sometimes, you want to fall back to some kind of default value, and |
| 20 | +/// don't particularly care what it is. This comes up often with `struct`s |
| 21 | +/// that define a set of options: |
91 | 22 | /// |
92 | | -/// [module]: ../../std/default/index.html |
| 23 | +/// ``` |
| 24 | +/// # #[allow(dead_code)] |
| 25 | +/// struct SomeOptions { |
| 26 | +/// foo: i32, |
| 27 | +/// bar: f32, |
| 28 | +/// } |
| 29 | +/// ``` |
| 30 | +/// |
| 31 | +/// How can we define some default values? You can use `Default`: |
| 32 | +/// |
| 33 | +/// ``` |
| 34 | +/// # #[allow(dead_code)] |
| 35 | +/// #[derive(Default)] |
| 36 | +/// struct SomeOptions { |
| 37 | +/// foo: i32, |
| 38 | +/// bar: f32, |
| 39 | +/// } |
| 40 | +/// |
| 41 | +/// |
| 42 | +/// fn main() { |
| 43 | +/// let options: SomeOptions = Default::default(); |
| 44 | +/// } |
| 45 | +/// ``` |
| 46 | +/// |
| 47 | +/// Now, you get all of the default values. Rust implements `Default` for various primitives types. |
| 48 | +/// |
| 49 | +/// If you want to override a particular option, but still retain the other defaults: |
| 50 | +/// |
| 51 | +/// ``` |
| 52 | +/// # #[allow(dead_code)] |
| 53 | +/// # #[derive(Default)] |
| 54 | +/// # struct SomeOptions { |
| 55 | +/// # foo: i32, |
| 56 | +/// # bar: f32, |
| 57 | +/// # } |
| 58 | +/// fn main() { |
| 59 | +/// let options = SomeOptions { foo: 42, ..Default::default() }; |
| 60 | +/// } |
| 61 | +/// ``` |
93 | 62 | /// |
94 | 63 | /// ## Derivable |
95 | 64 | /// |
|
0 commit comments