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 //! use std::default::Default;
29 //! #[deriving(Default)]
30 //! struct SomeOptions {
37 //! let options: SomeOptions = Default::default();
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:
45 //! use std::default::Default;
53 //! impl Default for Kind {
54 //! fn default() -> Kind { Kind::A }
57 //! #[deriving(Default)]
58 //! struct SomeOptions {
66 //! let options: SomeOptions = Default::default();
70 //! If you want to override a particular option, but still retain the other defaults:
73 //! # use std::default::Default;
74 //! # #[deriving(Default)]
75 //! # struct SomeOptions {
80 //! let options = SomeOptions { foo: 42, ..Default::default() };
86 /// A trait that types which have a useful default value should implement.
88 /// A struct can derive default implementations of `Default` for basic types using
89 /// `#[deriving(Default)]`.
94 /// #[deriving(Default)]
95 /// struct SomeOptions {
102 /// Returns the "default value" for a type.
104 /// Default values are often some kind of initial value, identity value, or anything else that
105 /// may make sense as a default.
109 /// Using built-in default values:
112 /// use std::default::Default;
114 /// let i: i8 = Default::default();
115 /// let (x, y): (Option<String>, f64) = Default::default();
116 /// let (a, b, (c, d)): (int, uint, (bool, bool)) = Default::default();
122 /// use std::default::Default;
130 /// impl Default for Kind {
131 /// fn default() -> Kind { Kind::A }
135 fn default() -> Self;
138 macro_rules! default_impl {
139 ($t:ty, $v:expr) => {
141 impl Default for $t {
144 fn default() -> $t { $v }
149 default_impl! { (), () }
150 default_impl! { bool, false }
151 default_impl! { char, '\x00' }
153 default_impl! { uint, 0u }
154 default_impl! { u8, 0u8 }
155 default_impl! { u16, 0u16 }
156 default_impl! { u32, 0u32 }
157 default_impl! { u64, 0u64 }
159 default_impl! { int, 0i }
160 default_impl! { i8, 0i8 }
161 default_impl! { i16, 0i16 }
162 default_impl! { i32, 0i32 }
163 default_impl! { i64, 0i64 }
165 default_impl! { f32, 0.0f32 }
166 default_impl! { f64, 0.0f64 }