Trait std::default::Default
pub trait Default { fn default() -> Self; }
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, }
How 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(); }
Now, 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() }; }
Derivable
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.
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 } }
Examples
#[derive(Default)] struct SomeOptions { foo: i32, bar: f32, }
Required methods
fn default() -> Self
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();
Making your own:
enum Kind { A, B, C, } impl Default for Kind { fn default() -> Self { Kind::A } }
Implementors
impl Default for &CStr
impl Default for &OsStr
impl Default for Global
impl Default for System
impl Default for Box<str, Global>
impl Default for Box<CStr>
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 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 Condvar
impl Default for Duration
impl<'_> Default for &'_ mut str
impl<'_, B> Default for Cow<'_, B> where
B: ToOwned + ?Sized,
<B as ToOwned>::Owned: Default,
impl<A, B, C, D, E> Default for (A, B, C, D, E) where
C: Default,
E: Default,
B: Default,
A: Default,
D: Default,
impl<A, B, C, D, E, F> Default for (A, B, C, D, E, F) where
C: Default,
F: Default,
E: Default,
B: Default,
A: Default,
D: Default,
impl<A, B, C, D, E, F, G> Default for (A, B, C, D, E, F, G) where
C: Default,
F: Default,
E: Default,
B: Default,
A: Default,
G: Default,
D: Default,
impl<A, B, C, D, E, F, G, H> Default for (A, B, C, D, E, F, G, H) where
C: Default,
F: Default,
E: Default,
H: Default,
B: Default,
A: Default,
G: Default,
D: Default,
impl<A, B, C, D, E, F, G, H, I> Default for (A, B, C, D, E, F, G, H, I) where
C: Default,
F: Default,
I: Default,
E: Default,
H: Default,
B: Default,
A: Default,
G: Default,
D: Default,
impl<A, B, C, D, E, F, G, H, I, J> Default for (A, B, C, D, E, F, G, H, I, J) where
C: Default,
F: Default,
I: Default,
E: Default,
H: Default,
B: Default,
A: Default,
J: Default,
G: Default,
D: Default,
impl<A, B, C, D, E, F, G, H, I, J, K> Default for (A, B, C, D, E, F, G, H, I, J, K) where
C: Default,
F: Default,
K: Default,
I: Default,
E: Default,
H: Default,
B: Default,
A: Default,
J: Default,
G: Default,
D: Default,
impl<A, B, C, D, E, F, G, H, I, J, K, L> Default for (A, B, C, D, E, F, G, H, I, J, K, L) where
C: Default,
F: Default,
K: Default,
I: Default,
E: Default,
H: Default,
B: Default,
A: Default,
J: Default,
G: Default,
D: Default,
L: Default,
impl<K, V> Default for BTreeMap<K, V>
impl<T> Default for Box<[T], Global>
impl<T> Default for BTreeSet<T>
impl<T> Default for LinkedList<T>
impl<T> Default for VecDeque<T, Global>
impl<T> Default for OnceCell<T>
impl<T> Default for SyncOnceCell<T>
impl<T> Default for std::rc::Weak<T>
impl<T> Default for std::sync::Weak<T>
impl<T: Default> Default for Cursor<T>
impl<T: Default> Default for SyncLazy<T>
impl<T: Default> Default for RwLock<T>
impl<T: ?Sized + Default> Default for Mutex<T>
© 2010 The Rust Project Developers
Licensed under the Apache License, Version 2.0 or the MIT license, at your option.
https://doc.rust-lang.org/std/default/trait.Default.html