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.
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,
}
You 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 }
}
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.7.0 · sourceimpl Default for RandomState
impl Default for RandomState
sourcefn default() -> RandomState
fn default() -> RandomState
Constructs a new RandomState
.
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.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.
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());
}
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<I, J, K, L> Default for (I, J, K, L) where
I: Default,
J: Default,
K: Default,
L: Default,
impl<I, J, K, L> Default for (I, J, K, L) where
I: Default,
J: Default,
K: Default,
L: Default,
sourceimpl<D, E, F, G, H, I, J, K, L> Default for (D, E, F, G, H, I, J, K, L) where
D: Default,
E: Default,
F: Default,
G: Default,
H: Default,
I: Default,
J: Default,
K: Default,
L: Default,
impl<D, E, F, G, H, I, J, K, L> Default for (D, E, F, G, H, I, J, K, L) where
D: Default,
E: Default,
F: Default,
G: Default,
H: Default,
I: Default,
J: Default,
K: Default,
L: Default,
sourceimpl<G, H, I, J, K, L> Default for (G, H, I, J, K, L) where
G: Default,
H: Default,
I: Default,
J: Default,
K: Default,
L: Default,
impl<G, H, I, J, K, L> Default for (G, H, I, J, K, L) where
G: Default,
H: Default,
I: Default,
J: Default,
K: Default,
L: Default,
sourceimpl<E, F, G, H, I, J, K, L> Default for (E, F, G, H, I, J, K, L) where
E: Default,
F: Default,
G: Default,
H: Default,
I: Default,
J: Default,
K: Default,
L: Default,
impl<E, F, G, H, I, J, K, L> Default for (E, F, G, H, I, J, K, L) where
E: Default,
F: Default,
G: Default,
H: Default,
I: Default,
J: Default,
K: Default,
L: Default,
1.5.0 (const: unstable) · sourceimpl<'_, T> Default for &'_ mut [T]
impl<'_, T> Default for &'_ mut [T]
sourceimpl<F, G, H, I, J, K, L> Default for (F, G, H, I, J, K, L) where
F: Default,
G: Default,
H: Default,
I: Default,
J: Default,
K: Default,
L: Default,
impl<F, G, H, I, J, K, L> Default for (F, G, H, I, J, K, L) where
F: Default,
G: Default,
H: Default,
I: Default,
J: Default,
K: Default,
L: Default,
sourceimpl<C, D, E, F, G, H, I, J, K, L> Default for (C, D, E, F, G, H, I, J, K, L) where
C: Default,
D: Default,
E: Default,
F: Default,
G: Default,
H: Default,
I: Default,
J: Default,
K: Default,
L: Default,
impl<C, D, E, F, G, H, I, J, K, L> Default for (C, D, E, F, G, H, I, J, K, L) where
C: Default,
D: Default,
E: Default,
F: Default,
G: Default,
H: Default,
I: Default,
J: Default,
K: Default,
L: Default,
sourceimpl<B, C, D, E, F, G, H, I, J, K, L> Default for (B, C, D, E, F, G, H, I, J, K, L) where
B: Default,
C: Default,
D: Default,
E: Default,
F: Default,
G: Default,
H: Default,
I: Default,
J: Default,
K: Default,
L: Default,
impl<B, C, D, E, F, G, H, I, J, K, L> Default for (B, C, D, E, F, G, H, I, J, K, L) where
B: Default,
C: Default,
D: Default,
E: Default,
F: Default,
G: Default,
H: Default,
I: Default,
J: Default,
K: Default,
L: Default,
sourceimpl<H, I, J, K, L> Default for (H, I, J, K, L) where
H: Default,
I: Default,
J: Default,
K: Default,
L: Default,
impl<H, I, J, K, L> Default for (H, I, J, K, L) where
H: Default,
I: Default,
J: Default,
K: Default,
L: Default,
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>
.
const: unstable · 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;
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>
.
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.
1.17.0 (const: unstable) · 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;
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,
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<T> Default for SymbolMap<T> where
T: Default + SymbolMapEntry,
impl<T> Default for SymbolMap<T> where
T: Default + SymbolMapEntry,
sourceimpl Default for LittleEndian
impl Default for LittleEndian
fn default() -> LittleEndian
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 Default for Endianness
impl Default for Endianness
fn default() -> Endianness
sourceimpl Default for VersionIndex
impl Default for VersionIndex
fn default() -> VersionIndex
sourceimpl Default for SymbolIndex
impl Default for SymbolIndex
fn default() -> SymbolIndex
sourceimpl Default for SectionIndex
impl Default for SectionIndex
fn default() -> SectionIndex
sourceimpl<'data> Default for ObjectMapEntry<'data>
impl<'data> Default for ObjectMapEntry<'data>
fn default() -> ObjectMapEntry<'data>
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, 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<E> Default for CompressionHeader32<E> where
E: Default + Endian,
impl<E> Default for CompressionHeader32<E> where
E: Default + Endian,
fn default() -> CompressionHeader32<E>
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<'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>
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<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.
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 Default for AHasher
impl Default for AHasher
Provides a default Hasher with fixed keys. This is typically used in conjunction with BuildHasherDefault to create [AHasher]s in order to hash the keys of the map.
Generally it is preferable to use [RandomState] instead, so that different hashmaps will have different keys. However if fixed keys are desireable this may be used instead.
Example
use std::hash::BuildHasherDefault;
use ahash::{AHasher, RandomState};
use std::collections::HashMap;
let mut map: HashMap<i32, i32, BuildHasherDefault<AHasher>> = HashMap::default();
map.insert(12, 34);
fn default() -> AHasher
fn default() -> AHasher
Constructs a new [AHasher] with fixed keys.
If std
is enabled these will be generated upon first invocation.
Otherwise if the compile-time-rng
feature is enabled these will be generated at compile time.
If neither of these features are available, hardcoded constants will be used.
Because the values are fixed, different hashers will all hash elements the same way. This could make hash values predictable, if DOS attacks are a concern. If this behaviour is not required, it may be preferable to use [RandomState] instead.
Examples
use ahash::AHasher;
use std::hash::Hasher;
let mut hasher_1 = AHasher::default();
let mut hasher_2 = AHasher::default();
hasher_1.write_u32(1234);
hasher_2.write_u32(1234);
assert_eq!(hasher_1.finish(), hasher_2.finish());
sourceimpl Default for IgnoredAny
impl Default for IgnoredAny
fn default() -> IgnoredAny
sourceimpl Default for DefaultToHost
impl Default for DefaultToHost
fn default() -> DefaultToHost
sourceimpl Default for DefaultToUnknown
impl Default for DefaultToUnknown
fn default() -> DefaultToUnknown
sourceimpl<R> Default for DebugLocLists<R> where
R: Default,
impl<R> Default for DebugLocLists<R> where
R: Default,
fn default() -> DebugLocLists<R>
sourceimpl Default for LineEncoding
impl Default for LineEncoding
fn default() -> LineEncoding
sourceimpl Default for SectionBaseAddresses
impl Default for SectionBaseAddresses
fn default() -> SectionBaseAddresses
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 DebugTypes<R> where
R: Default,
impl<R> Default for DebugTypes<R> where
R: Default,
fn default() -> DebugTypes<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,
fn default() -> EndianSlice<'input, Endian>
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 DwarfFileType
impl Default for DwarfFileType
fn default() -> DwarfFileType
sourceimpl<R> Default for DebugLineStr<R> where
R: Default,
impl<R> Default for DebugLineStr<R> where
R: Default,
fn default() -> DebugLineStr<R>
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 Default for RunTimeEndian
impl Default for RunTimeEndian
fn default() -> RunTimeEndian
sourceimpl Default for Augmentation
impl Default for Augmentation
fn default() -> Augmentation
sourceimpl Default for BaseAddresses
impl Default for BaseAddresses
fn default() -> BaseAddresses
sourceimpl<R> Default for DebugAbbrev<R> where
R: Default,
impl<R> Default for DebugAbbrev<R> where
R: Default,
fn default() -> DebugAbbrev<R>
sourceimpl Default for Abbreviations
impl Default for Abbreviations
fn default() -> Abbreviations
sourceimpl<R> Default for DebugAranges<R> where
R: Default,
impl<R> Default for DebugAranges<R> where
R: Default,
fn default() -> DebugAranges<R>
sourceimpl<R> Default for DebugRanges<R> where
R: Default,
impl<R> Default for DebugRanges<R> where
R: Default,
fn default() -> DebugRanges<R>
sourceimpl<R> Default for DebugCuIndex<R> where
R: Default,
impl<R> Default for DebugCuIndex<R> where
R: Default,
fn default() -> DebugCuIndex<R>
sourceimpl<R> Default for DebugTuIndex<R> where
R: Default,
impl<R> Default for DebugTuIndex<R> where
R: Default,
fn default() -> DebugTuIndex<R>
sourceimpl<R> Default for DebugRngLists<R> where
R: Default,
impl<R> Default for DebugRngLists<R> where
R: Default,
fn default() -> DebugRngLists<R>
sourceimpl Default for LittleEndian
impl Default for LittleEndian
fn default() -> LittleEndian
sourceimpl<R> Default for RangeLists<R> where
R: Default,
impl<R> Default for RangeLists<R> where
R: Default,
fn default() -> RangeLists<R>
sourceimpl<R> Default for DebugStrOffsets<R> where
R: Default,
impl<R> Default for DebugStrOffsets<R> where
R: Default,
fn default() -> DebugStrOffsets<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 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<'_> Default for &'_ CStr
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.