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.17.0 · sourceimpl Default for Box<CStr, Global>
impl Default for Box<CStr, Global>
pub 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;
1.17.0 · sourceimpl Default for Box<OsStr, Global>
impl Default for Box<OsStr, Global>
pub 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;
const: unstable · sourceimpl<T> Default for SyncOnceCell<T>
impl<T> Default for SyncOnceCell<T>
const: unstable · sourcepub fn default() -> SyncOnceCell<T>
pub 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.13.0 · sourceimpl Default for DefaultHasher
impl Default for DefaultHasher
sourcepub fn default() -> DefaultHasher
pub fn default() -> DefaultHasher
Creates a new DefaultHasher
using new
.
See its documentation for more.
1.7.0 · sourceimpl Default for RandomState
impl Default for RandomState
sourcepub fn default() -> RandomState
pub fn default() -> RandomState
Constructs a new RandomState
.
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> 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> 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,
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, 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, 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, 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,
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> 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,
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 LinkedList<T>
impl<T> Default for LinkedList<T>
sourcepub fn default() -> LinkedList<T>
pub fn default() -> LinkedList<T>
Creates an empty LinkedList<T>
.
1.17.0 · sourceimpl Default for Box<str, Global>
impl Default for Box<str, Global>
pub 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,
sourcepub fn default() -> BinaryHeap<T>
pub 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,
sourcepub 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;
pub 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<T> Default for Box<[T], Global>
impl<T> Default for Box<[T], Global>
pub 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;
sourceimpl Default for SymbolIndex
impl Default for SymbolIndex
pub fn default() -> SymbolIndex
sourceimpl<'data, R> Default for StringTable<'data, R> where
R: ReadRef<'data>,
impl<'data, R> Default for StringTable<'data, R> where
R: ReadRef<'data>,
pub fn default() -> StringTable<'data, R>
sourceimpl Default for VersionIndex
impl Default for VersionIndex
pub fn default() -> VersionIndex
sourceimpl<'data, Elf> Default for VersionTable<'data, Elf> where
Elf: FileHeader,
impl<'data, Elf> Default for VersionTable<'data, Elf> where
Elf: FileHeader,
pub fn default() -> VersionTable<'data, Elf>
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>,
pub fn default() -> SymbolTable<'data, Elf, R>
sourceimpl<T> Default for SymbolMap<T> where
T: Default + SymbolMapEntry,
impl<T> Default for SymbolMap<T> where
T: Default + SymbolMapEntry,
sourceimpl Default for SectionIndex
impl Default for SectionIndex
pub fn default() -> SectionIndex
sourceimpl Default for LittleEndian
impl Default for LittleEndian
pub fn default() -> LittleEndian
sourceimpl<E> Default for CompressionHeader64<E> where
E: Default + Endian,
impl<E> Default for CompressionHeader64<E> where
E: Default + Endian,
pub fn default() -> CompressionHeader64<E>
sourceimpl Default for Endianness
impl Default for Endianness
pub fn default() -> Endianness
sourceimpl<E> Default for CompressionHeader32<E> where
E: Default + Endian,
impl<E> Default for CompressionHeader32<E> where
E: Default + Endian,
pub fn default() -> CompressionHeader32<E>
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,
pub fn default() -> SectionTable<'data, Elf, R>
sourceimpl<'data> Default for ObjectMapEntry<'data>
impl<'data> Default for ObjectMapEntry<'data>
pub fn default() -> ObjectMapEntry<'data>
sourceimpl Default for IgnoredAny
impl Default for IgnoredAny
pub fn default() -> IgnoredAny
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,
pub fn default() -> HashSet<T, S, A>
pub fn default() -> HashSet<T, S, A>
Creates an empty HashSet<T, S>
with the Default
value for the hasher.
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,
pub fn default() -> RawTable<T, A>
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,
pub fn default() -> HashMap<K, V, S, A>
pub 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
pub fn default() -> DefaultToUnknown
sourceimpl Default for DefaultToHost
impl Default for DefaultToHost
pub fn default() -> DefaultToHost
sourceimpl<R> Default for DebugRanges<R> where
R: Default,
impl<R> Default for DebugRanges<R> where
R: Default,
pub fn default() -> DebugRanges<R>
sourceimpl Default for Augmentation
impl Default for Augmentation
pub fn default() -> Augmentation
sourceimpl<R> Default for DebugTypes<R> where
R: Default,
impl<R> Default for DebugTypes<R> where
R: Default,
pub fn default() -> DebugTypes<R>
sourceimpl<R> Default for DebugLocLists<R> where
R: Default,
impl<R> Default for DebugLocLists<R> where
R: Default,
pub fn default() -> DebugLocLists<R>
sourceimpl<R> Default for DebugTuIndex<R> where
R: Default,
impl<R> Default for DebugTuIndex<R> where
R: Default,
pub fn default() -> DebugTuIndex<R>
sourceimpl<R> Default for DebugAranges<R> where
R: Default,
impl<R> Default for DebugAranges<R> where
R: Default,
pub fn default() -> DebugAranges<R>
sourceimpl<R> Default for DebugAbbrev<R> where
R: Default,
impl<R> Default for DebugAbbrev<R> where
R: Default,
pub fn default() -> DebugAbbrev<R>
sourceimpl Default for BaseAddresses
impl Default for BaseAddresses
pub fn default() -> BaseAddresses
sourceimpl Default for RunTimeEndian
impl Default for RunTimeEndian
pub fn default() -> RunTimeEndian
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>,
pub fn default() -> UnwindContext<R, A>
sourceimpl<R> Default for DebugLineStr<R> where
R: Default,
impl<R> Default for DebugLineStr<R> where
R: Default,
pub fn default() -> DebugLineStr<R>
sourceimpl Default for LineEncoding
impl Default for LineEncoding
pub fn default() -> LineEncoding
sourceimpl<R> Default for LocationLists<R> where
R: Default,
impl<R> Default for LocationLists<R> where
R: Default,
pub fn default() -> LocationLists<R>
sourceimpl<'input, Endian> Default for EndianSlice<'input, Endian> where
Endian: Default + Endianity,
impl<'input, Endian> Default for EndianSlice<'input, Endian> where
Endian: Default + Endianity,
pub fn default() -> EndianSlice<'input, Endian>
sourceimpl<R> Default for DebugRngLists<R> where
R: Default,
impl<R> Default for DebugRngLists<R> where
R: Default,
pub fn default() -> DebugRngLists<R>
sourceimpl<R> Default for RangeLists<R> where
R: Default,
impl<R> Default for RangeLists<R> where
R: Default,
pub fn default() -> RangeLists<R>
sourceimpl Default for SectionBaseAddresses
impl Default for SectionBaseAddresses
pub fn default() -> SectionBaseAddresses
sourceimpl Default for LittleEndian
impl Default for LittleEndian
pub fn default() -> LittleEndian
sourceimpl Default for Abbreviations
impl Default for Abbreviations
pub fn default() -> Abbreviations
sourceimpl<R> Default for DebugStrOffsets<R> where
R: Default,
impl<R> Default for DebugStrOffsets<R> where
R: Default,
pub fn default() -> DebugStrOffsets<R>
sourceimpl<R> Default for DebugCuIndex<R> where
R: Default,
impl<R> Default for DebugCuIndex<R> where
R: Default,
pub fn default() -> DebugCuIndex<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>,
pub fn default() -> UnwindTableRow<R, S>
sourceimpl Default for DwarfFileType
impl Default for DwarfFileType
pub fn default() -> DwarfFileType
Implementors
impl Default for MemoryInitialization
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.