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.
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.
11 //! The `Default` trait for types which may have meaningful default values.
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:
18 //! struct SomeOptions {
24 //! How can we define some default values? You can use `Default`:
27 //! #[derive(Default)]
28 //! struct SomeOptions {
35 //! let options: SomeOptions = Default::default();
39 //! Now, you get all of the default values. Rust implements `Default` for various primitives types.
40 //! If you have your own type, you need to implement `Default` yourself:
49 //! impl Default for Kind {
50 //! fn default() -> Kind { Kind::A }
53 //! #[derive(Default)]
54 //! struct SomeOptions {
62 //! let options: SomeOptions = Default::default();
66 //! If you want to override a particular option, but still retain the other defaults:
69 //! # #[derive(Default)]
70 //! # struct SomeOptions {
75 //! let options = SomeOptions { foo: 42, ..Default::default() };
79 #![stable(feature = "rust1", since = "1.0.0")]
81 /// A trait for giving a type a useful default value.
83 /// A struct can derive default implementations of `Default` for basic types using
84 /// `#[derive(Default)]`.
89 /// #[derive(Default)]
90 /// struct SomeOptions {
95 #[stable(feature = "rust1", since = "1.0.0")]
97 /// Returns the "default value" for a type.
99 /// Default values are often some kind of initial value, identity value, or anything else that
100 /// may make sense as a default.
104 /// Using built-in default values:
107 /// let i: i8 = Default::default();
108 /// let (x, y): (Option<String>, f64) = Default::default();
109 /// let (a, b, (c, d)): (i32, u32, (bool, bool)) = Default::default();
121 /// impl Default for Kind {
122 /// fn default() -> Kind { Kind::A }
125 #[stable(feature = "rust1", since = "1.0.0")]
126 fn default() -> Self;
129 macro_rules! default_impl {
130 ($t:ty, $v:expr) => {
131 #[stable(feature = "rust1", since = "1.0.0")]
132 impl Default for $t {
134 #[stable(feature = "rust1", since = "1.0.0")]
135 fn default() -> $t { $v }
140 default_impl! { (), () }
141 default_impl! { bool, false }
142 default_impl! { char, '\x00' }
144 default_impl! { usize, 0 }
145 default_impl! { u8, 0 }
146 default_impl! { u16, 0 }
147 default_impl! { u32, 0 }
148 default_impl! { u64, 0 }
150 default_impl! { isize, 0 }
151 default_impl! { i8, 0 }
152 default_impl! { i16, 0 }
153 default_impl! { i32, 0 }
154 default_impl! { i64, 0 }
156 default_impl! { f32, 0.0f32 }
157 default_impl! { f64, 0.0f64 }