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,
}
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
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 }
}
Implementations on Foreign Types
1.13.0 · sourceimpl Default for DefaultHasher
impl Default for DefaultHasher
sourcefn default() -> DefaultHasher
fn default() -> DefaultHasher
Creates a new DefaultHasher
using new
.
See its documentation for more.
1.17.0 · sourceimpl Default for Box<OsStr, Global>
impl Default for Box<OsStr, Global>
fn default() -> Box<OsStr, Global>ⓘNotable traits for Box<I, A>impl<I, A> Iterator for Box<I, A> where
I: Iterator + ?Sized,
A: Allocator, type Item = <I as Iterator>::Item;impl<F, A> Future for Box<F, A> where
F: Future + Unpin + ?Sized,
A: Allocator + 'static, type Output = <F as Future>::Output;
I: Iterator + ?Sized,
A: Allocator, type Item = <I as Iterator>::Item;impl<F, A> Future for Box<F, A> where
F: Future + Unpin + ?Sized,
A: Allocator + 'static, type Output = <F as Future>::Output;
1.7.0 · sourceimpl Default for RandomState
impl Default for RandomState
sourcefn default() -> RandomState
fn default() -> RandomState
Constructs a new RandomState
.
const: unstable · sourceimpl<T> Default for SyncOnceCell<T>
impl<T> Default for SyncOnceCell<T>
const: unstable · sourcefn default() -> SyncOnceCell<T>
fn default() -> SyncOnceCell<T>
Creates a new empty cell.
Example
#![feature(once_cell)]
use std::lazy::SyncOnceCell;
fn main() {
assert_eq!(SyncOnceCell::<()>::new(), SyncOnceCell::default());
}
1.17.0 · sourceimpl Default for Box<CStr, Global>
impl Default for Box<CStr, Global>
fn default() -> Box<CStr, Global>ⓘNotable traits for Box<I, A>impl<I, A> Iterator for Box<I, A> where
I: Iterator + ?Sized,
A: Allocator, type Item = <I as Iterator>::Item;impl<F, A> Future for Box<F, A> where
F: Future + Unpin + ?Sized,
A: Allocator + 'static, type Output = <F as Future>::Output;
I: Iterator + ?Sized,
A: Allocator, type Item = <I as Iterator>::Item;impl<F, A> Future for Box<F, A> where
F: Future + Unpin + ?Sized,
A: Allocator + 'static, type Output = <F as Future>::Output;
sourceimpl<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, 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,
sourceimpl<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, 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,
sourceimpl<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<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,
sourceimpl<A, B, C, D> Default for (A, B, C, D) where
A: Default,
B: Default,
C: Default,
D: Default,
impl<A, B, C, D> Default for (A, B, C, D) where
A: Default,
B: Default,
C: Default,
D: Default,
sourceimpl<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<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,
1.5.0 (const: unstable) · sourceimpl<'_, T> Default for &'_ mut [T]
impl<'_, T> Default for &'_ mut [T]
sourceimpl<A, B, C, D, E> Default for (A, B, C, D, E) where
A: Default,
B: Default,
C: Default,
D: Default,
E: Default,
impl<A, B, C, D, E> Default for (A, B, C, D, E) where
A: Default,
B: Default,
C: Default,
D: Default,
E: Default,
sourceimpl<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<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,
sourceimpl<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<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,
sourceimpl<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<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,
sourceimpl<T> Default for LinkedList<T>
impl<T> Default for LinkedList<T>
sourcefn default() -> LinkedList<T>
fn default() -> LinkedList<T>
Creates an empty LinkedList<T>
.
1.11.0 · sourceimpl<'_, B> Default for Cow<'_, B> where
B: ToOwned + ?Sized,
<B as ToOwned>::Owned: Default,
impl<'_, B> Default for Cow<'_, B> where
B: ToOwned + ?Sized,
<B as ToOwned>::Owned: Default,
sourceimpl<T> Default for Box<[T], Global>
impl<T> Default for Box<[T], Global>
fn default() -> Box<[T], Global>ⓘNotable traits for Box<I, A>impl<I, A> Iterator for Box<I, A> where
I: Iterator + ?Sized,
A: Allocator, type Item = <I as Iterator>::Item;impl<F, A> Future for Box<F, A> where
F: Future + Unpin + ?Sized,
A: Allocator + 'static, type Output = <F as Future>::Output;
I: Iterator + ?Sized,
A: Allocator, type Item = <I as Iterator>::Item;impl<F, A> Future for Box<F, A> where
F: Future + Unpin + ?Sized,
A: Allocator + 'static, type Output = <F as Future>::Output;
1.17.0 · sourceimpl Default for Box<str, Global>
impl Default for Box<str, Global>
fn default() -> Box<str, Global>ⓘNotable traits for Box<I, A>impl<I, A> Iterator for Box<I, A> where
I: Iterator + ?Sized,
A: Allocator, type Item = <I as Iterator>::Item;impl<F, A> Future for Box<F, A> where
F: Future + Unpin + ?Sized,
A: Allocator + 'static, type Output = <F as Future>::Output;
I: Iterator + ?Sized,
A: Allocator, type Item = <I as Iterator>::Item;impl<F, A> Future for Box<F, A> where
F: Future + Unpin + ?Sized,
A: Allocator + 'static, type Output = <F as Future>::Output;
sourceimpl<T> Default for BinaryHeap<T> where
T: Ord,
impl<T> Default for BinaryHeap<T> where
T: Ord,
sourcefn default() -> BinaryHeap<T>
fn default() -> BinaryHeap<T>
Creates an empty BinaryHeap<T>
.
sourceimpl<T> Default for Box<T, Global> where
T: Default,
impl<T> Default for Box<T, Global> where
T: Default,
sourcefn default() -> Box<T, Global>ⓘNotable traits for Box<I, A>impl<I, A> Iterator for Box<I, A> where
I: Iterator + ?Sized,
A: Allocator, type Item = <I as Iterator>::Item;impl<F, A> Future for Box<F, A> where
F: Future + Unpin + ?Sized,
A: Allocator + 'static, type Output = <F as Future>::Output;
fn default() -> Box<T, Global>ⓘNotable traits for Box<I, A>impl<I, A> Iterator for Box<I, A> where
I: Iterator + ?Sized,
A: Allocator, type Item = <I as Iterator>::Item;impl<F, A> Future for Box<F, A> where
F: Future + Unpin + ?Sized,
A: Allocator + 'static, type Output = <F as Future>::Output;
I: Iterator + ?Sized,
A: Allocator, type Item = <I as Iterator>::Item;impl<F, A> Future for Box<F, A> where
F: Future + Unpin + ?Sized,
A: Allocator + 'static, type Output = <F as Future>::Output;
Creates a Box<T>
, with the Default
value for T.
sourceimpl Default for Endianness
impl Default for Endianness
fn default() -> Endianness
sourceimpl Default for LittleEndian
impl Default for LittleEndian
fn default() -> LittleEndian
sourceimpl<'data, Elf, R> Default for SectionTable<'data, Elf, R> where
Elf: Default + FileHeader,
R: Default + ReadRef<'data>,
<Elf as FileHeader>::SectionHeader: Default,
impl<'data, Elf, R> Default for SectionTable<'data, Elf, R> where
Elf: Default + FileHeader,
R: Default + ReadRef<'data>,
<Elf as FileHeader>::SectionHeader: Default,
fn default() -> SectionTable<'data, Elf, R>
sourceimpl<'data, R> Default for StringTable<'data, R> where
R: ReadRef<'data>,
impl<'data, R> Default for StringTable<'data, R> where
R: ReadRef<'data>,
fn default() -> StringTable<'data, R>
sourceimpl<'data> Default for ObjectMapEntry<'data>
impl<'data> Default for ObjectMapEntry<'data>
fn default() -> ObjectMapEntry<'data>
sourceimpl<T> Default for SymbolMap<T> where
T: Default + SymbolMapEntry,
impl<T> Default for SymbolMap<T> where
T: Default + SymbolMapEntry,
sourceimpl<E> Default for CompressionHeader32<E> where
E: Default + Endian,
impl<E> Default for CompressionHeader32<E> where
E: Default + Endian,
fn default() -> CompressionHeader32<E>
sourceimpl Default for VersionIndex
impl Default for VersionIndex
fn default() -> VersionIndex
sourceimpl<'data, Elf, R> Default for SymbolTable<'data, Elf, R> where
Elf: FileHeader,
R: ReadRef<'data>,
impl<'data, Elf, R> Default for SymbolTable<'data, Elf, R> where
Elf: FileHeader,
R: ReadRef<'data>,
fn default() -> SymbolTable<'data, Elf, R>
sourceimpl Default for SectionIndex
impl Default for SectionIndex
fn default() -> SectionIndex
sourceimpl Default for SymbolIndex
impl Default for SymbolIndex
fn default() -> SymbolIndex
sourceimpl<E> Default for CompressionHeader64<E> where
E: Default + Endian,
impl<E> Default for CompressionHeader64<E> where
E: Default + Endian,
fn default() -> CompressionHeader64<E>
sourceimpl<'data, Elf> Default for VersionTable<'data, Elf> where
Elf: FileHeader,
impl<'data, Elf> Default for VersionTable<'data, Elf> where
Elf: FileHeader,
fn default() -> VersionTable<'data, Elf>
sourceimpl Default for IgnoredAny
impl Default for IgnoredAny
fn default() -> IgnoredAny
impl<T, A> Default for RawTable<T, A> where
A: Allocator + Clone + Default,
impl<T, A> Default for RawTable<T, A> where
A: Allocator + Clone + Default,
fn default() -> RawTable<T, A>
impl<T, S, A> Default for HashSet<T, S, A> where
S: Default,
A: Default + Allocator + Clone,
impl<T, S, A> Default for HashSet<T, S, A> where
S: Default,
A: Default + Allocator + Clone,
fn default() -> HashSet<T, S, A>
fn default() -> HashSet<T, S, A>
Creates an empty HashSet<T, S>
with the Default
value for the hasher.
impl<K, V, S, A> Default for HashMap<K, V, S, A> where
S: Default,
A: Default + Allocator + Clone,
impl<K, V, S, A> Default for HashMap<K, V, S, A> where
S: Default,
A: Default + Allocator + Clone,
fn default() -> HashMap<K, V, S, A>
fn default() -> HashMap<K, V, S, A>
Creates an empty HashMap<K, V, S, A>
, with the Default
value for the hasher and allocator.
sourceimpl Default for DefaultToUnknown
impl Default for DefaultToUnknown
fn default() -> DefaultToUnknown
sourceimpl Default for DefaultToHost
impl Default for DefaultToHost
fn default() -> DefaultToHost
sourceimpl<R, A> Default for UnwindContext<R, A> where
R: Reader,
A: UnwindContextStorage<R>,
impl<R, A> Default for UnwindContext<R, A> where
R: Reader,
A: UnwindContextStorage<R>,
fn default() -> UnwindContext<R, A>
sourceimpl<R> Default for DebugRanges<R> where
R: Default,
impl<R> Default for DebugRanges<R> where
R: Default,
fn default() -> DebugRanges<R>
sourceimpl Default for DwarfFileType
impl Default for DwarfFileType
fn default() -> DwarfFileType
sourceimpl<R> Default for DebugAranges<R> where
R: Default,
impl<R> Default for DebugAranges<R> where
R: Default,
fn default() -> DebugAranges<R>
sourceimpl<R, S> Default for UnwindTableRow<R, S> where
R: Reader,
S: UnwindContextStorage<R>,
impl<R, S> Default for UnwindTableRow<R, S> where
R: Reader,
S: UnwindContextStorage<R>,
fn default() -> UnwindTableRow<R, S>
sourceimpl<R> Default for DebugTuIndex<R> where
R: Default,
impl<R> Default for DebugTuIndex<R> where
R: Default,
fn default() -> DebugTuIndex<R>
sourceimpl Default for RunTimeEndian
impl Default for RunTimeEndian
fn default() -> RunTimeEndian
sourceimpl Default for SectionBaseAddresses
impl Default for SectionBaseAddresses
fn default() -> SectionBaseAddresses
sourceimpl<R> Default for DebugCuIndex<R> where
R: Default,
impl<R> Default for DebugCuIndex<R> where
R: Default,
fn default() -> DebugCuIndex<R>
sourceimpl Default for BaseAddresses
impl Default for BaseAddresses
fn default() -> BaseAddresses
sourceimpl<R> Default for DebugLocLists<R> where
R: Default,
impl<R> Default for DebugLocLists<R> where
R: Default,
fn default() -> DebugLocLists<R>
sourceimpl<R> Default for DebugAbbrev<R> where
R: Default,
impl<R> Default for DebugAbbrev<R> where
R: Default,
fn default() -> DebugAbbrev<R>
sourceimpl<R> Default for RangeLists<R> where
R: Default,
impl<R> Default for RangeLists<R> where
R: Default,
fn default() -> RangeLists<R>
sourceimpl Default for LittleEndian
impl Default for LittleEndian
fn default() -> LittleEndian
sourceimpl<R> Default for DebugTypes<R> where
R: Default,
impl<R> Default for DebugTypes<R> where
R: Default,
fn default() -> DebugTypes<R>
sourceimpl Default for LineEncoding
impl Default for LineEncoding
fn default() -> LineEncoding
sourceimpl<R> Default for DebugRngLists<R> where
R: Default,
impl<R> Default for DebugRngLists<R> where
R: Default,
fn default() -> DebugRngLists<R>
sourceimpl<R> Default for LocationLists<R> where
R: Default,
impl<R> Default for LocationLists<R> where
R: Default,
fn default() -> LocationLists<R>
sourceimpl Default for Augmentation
impl Default for Augmentation
fn default() -> Augmentation
sourceimpl<R> Default for DebugStrOffsets<R> where
R: Default,
impl<R> Default for DebugStrOffsets<R> where
R: Default,
fn default() -> DebugStrOffsets<R>
sourceimpl Default for Abbreviations
impl Default for Abbreviations
fn default() -> Abbreviations
sourceimpl<'input, Endian> Default for EndianSlice<'input, Endian> where
Endian: Default + Endianity,
impl<'input, Endian> Default for EndianSlice<'input, Endian> where
Endian: Default + Endianity,
fn default() -> EndianSlice<'input, Endian>
sourceimpl<R> Default for DebugLineStr<R> where
R: Default,
impl<R> Default for DebugLineStr<R> where
R: Default,
fn default() -> DebugLineStr<R>
Implementors
impl Default for MemoryInitialization
impl Default for TableInitialization
impl Default for AddressMapSection
impl Default for FilePos
impl Default for FunctionInfo
impl Default for InstanceSignature
impl Default for Module
impl Default for TrapEncodingBuilder
impl Default for Tunables
impl Default for TypeTables
impl Default for WasmFileInfo
impl Default for Parser
impl Default for Validator
impl Default for WasmFeatures
impl Default for Error
impl Default for SipHasher
impl Default for PhantomPinned
impl Default for RangeFull
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 Duration
impl<'a> Default for DebugInfoData<'a>
impl<'a> Default for NameSection<'a>
impl<'data> Default for ModuleTranslation<'data>
impl<H> Default for BuildHasherDefault<H>
impl<Idx> Default for Range<Idx> where
Idx: Default,
impl<K, V> Default for PrimaryMap<K, V> where
K: EntityRef,
impl<K, V> Default for SecondaryMap<K, V> where
K: EntityRef,
V: Clone + Default,
impl<T> Default for Option<T>
impl<T> Default for PackedOption<T> where
T: ReservedValue,
impl<T> Default for EntityList<T> where
T: EntityRef + ReservedValue,
Create an empty list.