Skip to content

Commit 1b32298

Browse files
committed
Move all Default docs from module to trait
I had already copied the implementation example in a previous commit; this copies the explanation and usage examples to the general trait description.
1 parent d81a999 commit 1b32298

File tree

1 file changed

+42
-73
lines changed

1 file changed

+42
-73
lines changed

src/libcore/default.rs

Lines changed: 42 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -9,87 +9,56 @@
99
// except according to those terms.
1010

1111
//! 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-
//! ```
8212
8313
#![stable(feature = "rust1", since = "1.0.0")]
8414

8515
use marker::Sized;
8616

8717
/// A trait for giving a type a useful default value.
8818
///
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:
9122
///
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+
/// ```
9362
///
9463
/// ## Derivable
9564
///

0 commit comments

Comments
 (0)