rust/src/libcore/default.rs

58 lines
1.5 KiB
Rust
Raw Normal View History

2013-08-10 09:38:00 -04:00
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
//! The `Default` trait for types which may have meaningful default values
2013-08-10 09:38:00 -04:00
2014-07-18 16:01:55 -07:00
#![stable]
2013-08-10 09:38:00 -04:00
/// A trait that types which have a useful default value should implement.
pub trait Default {
/// Return the "default value" for a type.
2014-07-19 12:10:09 +02:00
///
/// # Example
///
/// ```
/// use std::default::Default;
///
/// let i: i8 = Default::default();
/// let (x, y): (Option<String>, f64) = Default::default();
/// let (a, b, (c, d)): (int, uint, (bool, bool)) = Default::default();
/// ```
2013-08-10 09:38:00 -04:00
fn default() -> Self;
}
2013-09-11 21:49:25 -07:00
macro_rules! default_impl(
($t:ty, $v:expr) => {
impl Default for $t {
#[inline]
fn default() -> $t { $v }
}
}
)
default_impl!((), ())
default_impl!(bool, false)
default_impl!(char, '\x00')
default_impl!(uint, 0u)
default_impl!(u8, 0u8)
default_impl!(u16, 0u16)
default_impl!(u32, 0u32)
default_impl!(u64, 0u64)
default_impl!(int, 0i)
default_impl!(i8, 0i8)
default_impl!(i16, 0i16)
default_impl!(i32, 0i32)
default_impl!(i64, 0i64)
default_impl!(f32, 0.0f32)
default_impl!(f64, 0.0f64)