Skip to content

Commit 3f8da69

Browse files
committed
auto merge of #17455 : steveklabnik/rust/document_default, r=alexcrichton
Given that the `Default` module is now stable, it might as well get good docs.
2 parents 58413c0 + 03b96d1 commit 3f8da69

File tree

1 file changed

+108
-3
lines changed

1 file changed

+108
-3
lines changed

src/libcore/default.rs

Lines changed: 108 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,104 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

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+
//! ```
1283
1384
#![stable]
1485

1586
/// 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+
/// ```
16100
pub trait Default {
17-
/// Return the "default value" for a type.
101+
/// Returns the "default value" for a type.
18102
///
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:
20109
///
21110
/// ```
22111
/// use std::default::Default;
@@ -25,6 +114,22 @@ pub trait Default {
25114
/// let (x, y): (Option<String>, f64) = Default::default();
26115
/// let (a, b, (c, d)): (int, uint, (bool, bool)) = Default::default();
27116
/// ```
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+
/// ```
28133
fn default() -> Self;
29134
}
30135

0 commit comments

Comments
 (0)