pub trait Default {
fn default() -> Self;
}
Expand description
A trait for giving a type a useful default value.
Sometimes, you want to fall back to some kind of default value, and
don’t particularly care what it is. This comes up often with struct
s
that define a set of options:
struct SomeOptions {
foo: i32,
bar: f32,
}
RunHow can we define some default values? You can use Default
:
#[derive(Default)]
struct SomeOptions {
foo: i32,
bar: f32,
}
fn main() {
let options: SomeOptions = Default::default();
}
RunNow, you get all of the default values. Rust implements Default
for various primitives types.
If you want to override a particular option, but still retain the other defaults:
fn main() {
let options = SomeOptions { foo: 42, ..Default::default() };
}
RunDerivable
This trait can be used with #[derive]
if all of the type’s fields implement
Default
. When derive
d, it will use the default value for each field’s type.
enum
s
When using #[derive(Default)]
on an enum
, you need to choose which unit variant will be
default. You do this by placing the #[default]
attribute on the variant.
#[derive(Default)]
enum Kind {
#[default]
A,
B,
C,
}
RunYou cannot use the #[default]
attribute on non-unit or non-exhaustive variants.
How can I implement Default
?
Provide an implementation for the default()
method that returns the value of
your type that should be the default:
enum Kind {
A,
B,
C,
}
impl Default for Kind {
fn default() -> Self { Kind::A }
}
RunExamples
#[derive(Default)]
struct SomeOptions {
foo: i32,
bar: f32,
}
RunRequired Methods
Returns the “default value” for a type.
Default values are often some kind of initial value, identity value, or anything else that may make sense as a default.
Examples
Using built-in default values:
let i: i8 = Default::default();
let (x, y): (Option<String>, f64) = Default::default();
let (a, b, (c, d)): (i32, u32, (bool, bool)) = Default::default();
RunMaking your own:
enum Kind {
A,
B,
C,
}
impl Default for Kind {
fn default() -> Self { Kind::A }
}
RunImplementors
impl Default for &str
impl Default for &CStr
impl Default for &OsStr
impl Default for &mut str
impl Default for bool
impl Default for char
impl Default for f32
impl Default for f64
impl Default for i8
impl Default for i16
impl Default for i32
impl Default for i64
impl Default for i128
impl Default for isize
impl Default for u8
impl Default for u16
impl Default for u32
impl Default for u64
impl Default for u128
impl Default for ()
impl Default for usize
impl Default for Global
impl Default for System
impl Default for Box<str, Global>
impl Default for Box<CStr, Global>
impl Default for Box<OsStr>
impl Default for DefaultHasher
impl Default for RandomState
impl Default for CString
impl Default for OsString
impl Default for Error
impl Default for FileTimes
impl Default for SipHasher
impl Default for std::io::Empty
impl Default for Sink
impl Default for PhantomPinned
impl Default for RangeFull
impl Default for PathBuf
impl Default for String
impl Default for AtomicBool
impl Default for AtomicI8
impl Default for AtomicI16
impl Default for AtomicI32
impl Default for AtomicI64
impl Default for AtomicIsize
impl Default for AtomicU8
impl Default for AtomicU16
impl Default for AtomicU32
impl Default for AtomicU64
impl Default for AtomicUsize
impl Default for Condvar
impl Default for Duration
impl<B> Default for Cow<'_, B> where
B: ToOwned + ?Sized,
<B as ToOwned>::Owned: Default,
impl<H> Default for BuildHasherDefault<H>
impl<Idx> Default for Range<Idx> where
Idx: Default,
impl<K, V> Default for BTreeMap<K, V, Global>
impl<K, V, S> Default for HashMap<K, V, S> where
S: Default,
impl<T> Default for &[T]
impl<T> Default for &mut [T]
impl<T> Default for Option<T>
impl<T> Default for [T; 0]
impl<T> Default for [T; 1] where
T: Default,
impl<T> Default for [T; 2] where
T: Default,
impl<T> Default for [T; 3] where
T: Default,
impl<T> Default for [T; 4] where
T: Default,
impl<T> Default for [T; 5] where
T: Default,
impl<T> Default for [T; 6] where
T: Default,
impl<T> Default for [T; 7] where
T: Default,
impl<T> Default for [T; 8] where
T: Default,
impl<T> Default for [T; 9] where
T: Default,
impl<T> Default for [T; 10] where
T: Default,
impl<T> Default for [T; 11] where
T: Default,
impl<T> Default for [T; 12] where
T: Default,
impl<T> Default for [T; 13] where
T: Default,
impl<T> Default for [T; 14] where
T: Default,
impl<T> Default for [T; 15] where
T: Default,
impl<T> Default for [T; 16] where
T: Default,
impl<T> Default for [T; 17] where
T: Default,
impl<T> Default for [T; 18] where
T: Default,
impl<T> Default for [T; 19] where
T: Default,
impl<T> Default for [T; 20] where
T: Default,
impl<T> Default for [T; 21] where
T: Default,
impl<T> Default for [T; 22] where
T: Default,
impl<T> Default for [T; 23] where
T: Default,
impl<T> Default for [T; 24] where
T: Default,
impl<T> Default for [T; 25] where
T: Default,
impl<T> Default for [T; 26] where
T: Default,
impl<T> Default for [T; 27] where
T: Default,
impl<T> Default for [T; 28] where
T: Default,
impl<T> Default for [T; 29] where
T: Default,
impl<T> Default for [T; 30] where
T: Default,
impl<T> Default for [T; 31] where
T: Default,
impl<T> Default for [T; 32] where
T: Default,
impl<T> Default for (T₁, T₂, …, Tₙ) where
T: Default,
This trait is implemented for tuples up to twelve items long.