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() }; }
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.
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() -> Kind { Kind::A } }
#[derive(Default)] struct SomeOptions { foo: i32, bar: f32, }
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.
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() -> Kind { Kind::A } }
impl<T> Default for Box<T> where
T: Default,
impl<'a, B> Default for Cow<'a, B> where
B: ToOwned + ?Sized,
<B as ToOwned>::Owned: Default,
impl<T> Default for std::rc::Weak<T>
impl<T> Default for BinaryHeap<T> where
T: Ord,
impl<T> Default for Rc<T> where
T: Default,
impl<K, V> Default for BTreeMap<K, V> where
K: Ord,
impl Default for Box<str>
impl Default for Heap
impl<T> Default for Arc<T> where
T: Default,
impl<T> Default for VecDeque<T>
impl<T> Default for Vec<T>
impl<T> Default for std::sync::Weak<T>
impl<T> Default for Box<[T]>
impl Default for String
impl<T> Default for BTreeSet<T> where
T: Ord,
impl<T> Default for LinkedList<T>
impl Default for i16
impl Default for SipHasher13
impl Default for i64
impl<T> Default for [T; 32 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1
- 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1] where
T: Default,
impl<A> Default for (A,) where
A: 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
A: Default,
B: Default,
C: Default,
D: Default,
E: Default,
F: Default,
G: Default,
H: Default,
I: Default,
J: Default,
K: Default,
L: Default,
impl<T> Default for Wrapping<T> where
T: Default,
impl<T> Default for [T; 32 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1
- 1 - 1 - 1] where
T: Default,
impl Default for AtomicU64
impl Default for char
impl<T> Default for [T; 32 - 1 - 1 - 1 - 1 - 1 - 1] where
T: Default,
impl Default for i8
impl Default for SipHasher24
impl Default for isize
impl<T> Default for [T; 32] where
T: Default,
impl<T> Default for [T; 32 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1] where
T: Default,
impl<T> Default for [T; 32 - 1 - 1] where
T: Default,
impl Default for AtomicI32
impl<T> Default for ManuallyDrop<T> where
T: Default,
impl Default for AtomicI8
impl<'a> Default for &'a str
impl Default for AtomicU8
impl Default for u16
impl<T> Default for [T; 32 - 1 - 1 - 1] where
T: Default,
impl Default for AtomicIsize
impl<A, B, C, D> Default for (A, B, C, D) where
A: Default,
B: Default,
C: Default,
D: Default,
impl<T> Default for [T; 32 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1
- 1 - 1 - 1 - 1 - 1] where
T: Default,
impl<T> Default for [T; 32 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1
- 1 - 1 - 1 - 1 - 1 - 1] where
T: Default,
impl<T> Default for [T; 32 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1] where
T: Default,
impl<T> Default for [T; 32 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1] where
T: Default,
impl<T> Default for [T; 32 - 1] where
T: Default,
impl Default for ()
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
A: Default,
B: Default,
C: Default,
D: Default,
E: Default,
F: Default,
G: Default,
H: Default,
I: Default,
J: Default,
K: Default,
impl<T> Default for [T; 32 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1
- 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1] where
T: Default,
impl<T> Default for [T; 32 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1] where
T: Default,
impl Default for usize
impl Default for i128
impl Default for AtomicUsize
impl Default for bool
impl<T> Default for [T; 32 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1
- 1 - 1] where
T: Default,
impl Default for u128
impl<T> Default for [T; 32 - 1 - 1 - 1 - 1] where
T: Default,
impl<T> Default for [T; 32 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1
- 1 - 1 - 1 - 1 - 1 - 1 - 1] where
T: Default,
impl Default for Error
impl<T> Default for [T; 32 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1
- 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1]
impl<T> Default for PhantomData<T> where
T: ?Sized,
impl<T> Default for Empty<T>
impl Default for SipHasher
impl<T> Default for [T; 32 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1] where
T: Default,
impl<T> Default for [T; 32 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1
- 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1] where
T: Default,
impl Default for i32
impl Default for u8
impl<T> Default for [T; 32 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1
- 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1] where
T: Default,
impl<T> Default for RefCell<T> where
T: Default,
impl<A, B, C, D, E, F, G, H> Default for (A, B, C, D, E, F, G, H) where
A: Default,
B: Default,
C: Default,
D: Default,
E: Default,
F: Default,
G: Default,
H: Default,
impl<T> Default for Option<T>
impl<T> Default for [T; 32 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1] where
T: Default,
impl<'a, T> Default for &'a mut [T]
impl<A, B> Default for (A, B) where
A: Default,
B: Default,
impl<T> Default for UnsafeCell<T> where
T: Default,
impl<T> Default for [T; 32 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1] where
T: Default,
impl Default for u32
impl<T> Default for [T; 32 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1] where
T: Default,
impl Default for AtomicU16
impl Default for AtomicBool
impl Default for AtomicU32
impl<T> Default for [T; 32 - 1 - 1 - 1 - 1 - 1] where
T: Default,
impl<T> Default for [T; 32 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1
- 1 - 1 - 1 - 1] where
T: Default,
impl<T> Default for [T; 32 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1
- 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1] where
T: Default,
impl<A, B, C, D, E, F, G> Default for (A, B, C, D, E, F, G) where
A: Default,
B: Default,
C: Default,
D: Default,
E: Default,
F: Default,
G: Default,
impl<A, B, C, D, E, F> Default for (A, B, C, D, E, F) where
A: Default,
B: Default,
C: Default,
D: Default,
E: Default,
F: Default,
impl<T> Default for [T; 32 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1] where
T: Default,
impl<T> Default for [T; 32 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1] where
T: Default,
impl Default for f32
impl Default for AtomicI16
impl<T> Default for [T; 32 - 1 - 1 - 1 - 1 - 1 - 1 - 1] where
T: Default,
impl<A, B, C, D, E, F, G, H, I> Default for (A, B, C, D, E, F, G, H, I) where
A: Default,
B: Default,
C: Default,
D: Default,
E: Default,
F: Default,
G: Default,
H: Default,
I: Default,
impl<'a, T> Default for &'a [T]
impl<T> Default for Cell<T> where
T: Default,
impl Default for u64
impl<A, B, C, D, E> Default for (A, B, C, D, E) where
A: Default,
B: Default,
C: Default,
D: Default,
E: Default,
impl Default for f64
impl<T> Default for [T; 32 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1
- 1] where
T: Default,
impl<T> Default for [T; 32 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1] where
T: Default,
impl<H> Default for BuildHasherDefault<H>
impl<A, B, C, D, E, F, G, H, I, J> Default for (A, B, C, D, E, F, G, H, I, J) where
A: Default,
B: Default,
C: Default,
D: Default,
E: Default,
F: Default,
G: Default,
H: Default,
I: Default,
J: Default,
impl<T> Default for [T; 32 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1] where
T: Default,
impl<A, B, C> Default for (A, B, C) where
A: Default,
B: Default,
C: Default,
impl Default for AtomicI64
impl<T> Default for AtomicPtr<T>
impl<K, V, S> Default for HashMap<K, V, S> where
K: Eq + Hash,
S: BuildHasher + Default,
impl Default for DefaultHasher
impl Default for RandomState
impl<T, S> Default for HashSet<T, S> where
T: Eq + Hash,
S: BuildHasher + Default,
impl<'a> Default for &'a CStr
impl Default for CString
impl Default for Box<CStr>
impl Default for OsString
impl Default for Box<OsStr>
impl<'a> Default for &'a OsStr
impl Default for PathBuf
impl Default for Condvar
impl<T: ?Sized + Default> Default for Mutex<T>
impl<T: Default> Default for RwLock<T>
impl Default for Duration
© 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