]> git.lizzy.rs Git - rust.git/blob - src/libcore/default.rs
auto merge of #15806 : treeman/rust/std-doc, r=alexcrichton
[rust.git] / src / libcore / default.rs
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
12
13 #![stable]
14
15 /// A trait that types which have a useful default value should implement.
16 pub trait Default {
17     /// Return the "default value" for a type.
18     ///
19     /// # Example
20     ///
21     /// ```
22     /// use std::default::Default;
23     ///
24     /// let i: i8 = Default::default();
25     /// let (x, y): (Option<String>, f64) = Default::default();
26     /// let (a, b, (c, d)): (int, uint, (bool, bool)) = Default::default();
27     /// ```
28     fn default() -> Self;
29 }
30
31 macro_rules! default_impl(
32     ($t:ty, $v:expr) => {
33         impl Default for $t {
34             #[inline]
35             fn default() -> $t { $v }
36         }
37     }
38 )
39
40 default_impl!((), ())
41 default_impl!(bool, false)
42 default_impl!(char, '\x00')
43
44 default_impl!(uint, 0u)
45 default_impl!(u8,  0u8)
46 default_impl!(u16, 0u16)
47 default_impl!(u32, 0u32)
48 default_impl!(u64, 0u64)
49
50 default_impl!(int, 0i)
51 default_impl!(i8,  0i8)
52 default_impl!(i16, 0i16)
53 default_impl!(i32, 0i32)
54 default_impl!(i64, 0i64)
55
56 default_impl!(f32, 0.0f32)
57 default_impl!(f64, 0.0f64)