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 //! # #[allow(dead_code)]
19 //! struct SomeOptions {
25 //! How can we define some default values? You can use `Default`:
28 //! # #[allow(dead_code)]
29 //! #[derive(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 //! # #![allow(dead_code)]
52 //! impl Default for Kind {
53 //! fn default() -> Kind { Kind::A }
56 //! #[derive(Default)]
57 //! struct SomeOptions {
65 //! let options: SomeOptions = Default::default();
69 //! If you want to override a particular option, but still retain the other defaults:
72 //! # #[allow(dead_code)]
73 //! # #[derive(Default)]
74 //! # struct SomeOptions {
79 //! let options = SomeOptions { foo: 42, ..Default::default() };
83 #![stable(feature = "rust1", since = "1.0.0")]
87 /// A trait for giving a type a useful default value.
89 /// A struct can derive default implementations of `Default` for basic types using
90 /// `#[derive(Default)]`.
95 /// # #[allow(dead_code)]
96 /// #[derive(Default)]
97 /// struct SomeOptions {
102 #[stable(feature = "rust1", since = "1.0.0")]
103 pub trait Default: Sized {
104 /// Returns the "default value" for a type.
106 /// Default values are often some kind of initial value, identity value, or anything else that
107 /// may make sense as a default.
111 /// Using built-in default values:
114 /// let i: i8 = Default::default();
115 /// let (x, y): (Option<String>, f64) = Default::default();
116 /// let (a, b, (c, d)): (i32, u32, (bool, bool)) = Default::default();
122 /// # #[allow(dead_code)]
129 /// impl Default for Kind {
130 /// fn default() -> Kind { Kind::A }
133 #[stable(feature = "rust1", since = "1.0.0")]
134 fn default() -> Self;
137 macro_rules! default_impl {
138 ($t:ty, $v:expr) => {
139 #[stable(feature = "rust1", since = "1.0.0")]
140 impl Default for $t {
142 fn default() -> $t { $v }
147 default_impl! { (), () }
148 default_impl! { bool, false }
149 default_impl! { char, '\x00' }
151 default_impl! { usize, 0 }
152 default_impl! { u8, 0 }
153 default_impl! { u16, 0 }
154 default_impl! { u32, 0 }
155 default_impl! { u64, 0 }
157 default_impl! { isize, 0 }
158 default_impl! { i8, 0 }
159 default_impl! { i16, 0 }
160 default_impl! { i32, 0 }
161 default_impl! { i64, 0 }
163 default_impl! { f32, 0.0f32 }
164 default_impl! { f64, 0.0f64 }