]> git.proxmox.com Git - rustc.git/blame - src/libcore/default.rs
New upstream version 1.13.0+dfsg1
[rustc.git] / src / libcore / default.rs
CommitLineData
1a4d82fc
JJ
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.
1a4d82fc 12
85aaf69f 13#![stable(feature = "rust1", since = "1.0.0")]
1a4d82fc 14
e9174d1e 15/// A trait for giving a type a useful default value.
1a4d82fc 16///
3157f602
XL
17/// Sometimes, you want to fall back to some kind of default value, and
18/// don't particularly care what it is. This comes up often with `struct`s
19/// that define a set of options:
20///
21/// ```
22/// # #[allow(dead_code)]
23/// struct SomeOptions {
24/// foo: i32,
25/// bar: f32,
26/// }
27/// ```
28///
29/// How can we define some default values? You can use `Default`:
30///
31/// ```
32/// # #[allow(dead_code)]
33/// #[derive(Default)]
34/// struct SomeOptions {
35/// foo: i32,
36/// bar: f32,
37/// }
38///
3157f602
XL
39/// fn main() {
40/// let options: SomeOptions = Default::default();
41/// }
42/// ```
43///
44/// Now, you get all of the default values. Rust implements `Default` for various primitives types.
45///
46/// If you want to override a particular option, but still retain the other defaults:
47///
48/// ```
49/// # #[allow(dead_code)]
50/// # #[derive(Default)]
51/// # struct SomeOptions {
52/// # foo: i32,
53/// # bar: f32,
54/// # }
55/// fn main() {
56/// let options = SomeOptions { foo: 42, ..Default::default() };
57/// }
58/// ```
59///
60/// ## Derivable
61///
62/// This trait can be used with `#[derive]` if all of the type's fields implement
63/// `Default`. When `derive`d, it will use the default value for each field's type.
64///
65/// ## How can I implement `Default`?
66///
67/// Provide an implementation for the `default()` method that returns the value of
68/// your type that should be the default:
69///
70/// ```
71/// # #![allow(dead_code)]
72/// enum Kind {
73/// A,
74/// B,
75/// C,
76/// }
77///
78/// impl Default for Kind {
79/// fn default() -> Kind { Kind::A }
80/// }
81/// ```
1a4d82fc
JJ
82///
83/// # Examples
84///
85/// ```
92a42be0 86/// # #[allow(dead_code)]
1a4d82fc
JJ
87/// #[derive(Default)]
88/// struct SomeOptions {
85aaf69f 89/// foo: i32,
1a4d82fc
JJ
90/// bar: f32,
91/// }
92/// ```
85aaf69f 93#[stable(feature = "rust1", since = "1.0.0")]
b039eaaf 94pub trait Default: Sized {
1a4d82fc
JJ
95 /// Returns the "default value" for a type.
96 ///
97 /// Default values are often some kind of initial value, identity value, or anything else that
98 /// may make sense as a default.
99 ///
100 /// # Examples
101 ///
102 /// Using built-in default values:
103 ///
104 /// ```
1a4d82fc
JJ
105 /// let i: i8 = Default::default();
106 /// let (x, y): (Option<String>, f64) = Default::default();
85aaf69f 107 /// let (a, b, (c, d)): (i32, u32, (bool, bool)) = Default::default();
1a4d82fc
JJ
108 /// ```
109 ///
110 /// Making your own:
111 ///
112 /// ```
92a42be0 113 /// # #[allow(dead_code)]
1a4d82fc
JJ
114 /// enum Kind {
115 /// A,
116 /// B,
117 /// C,
118 /// }
119 ///
120 /// impl Default for Kind {
121 /// fn default() -> Kind { Kind::A }
122 /// }
123 /// ```
85aaf69f 124 #[stable(feature = "rust1", since = "1.0.0")]
1a4d82fc
JJ
125 fn default() -> Self;
126}
127
128macro_rules! default_impl {
129 ($t:ty, $v:expr) => {
85aaf69f 130 #[stable(feature = "rust1", since = "1.0.0")]
1a4d82fc
JJ
131 impl Default for $t {
132 #[inline]
1a4d82fc
JJ
133 fn default() -> $t { $v }
134 }
135 }
136}
137
138default_impl! { (), () }
139default_impl! { bool, false }
140default_impl! { char, '\x00' }
141
85aaf69f
SL
142default_impl! { usize, 0 }
143default_impl! { u8, 0 }
144default_impl! { u16, 0 }
145default_impl! { u32, 0 }
146default_impl! { u64, 0 }
1a4d82fc 147
85aaf69f
SL
148default_impl! { isize, 0 }
149default_impl! { i8, 0 }
150default_impl! { i16, 0 }
151default_impl! { i32, 0 }
152default_impl! { i64, 0 }
1a4d82fc
JJ
153
154default_impl! { f32, 0.0f32 }
155default_impl! { f64, 0.0f64 }