pub trait Default:Sized { // Required method fndefault() -> 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, anddon’t particularly care what it is. This comes up often withstructsthat define a set of options:
How can we define some default values? You can useDefault:
#[derive(Default)]structSomeOptions { foo: i32, bar: f32,}fnmain() {letoptions: SomeOptions = Default::default();}Now, you get all of the default values. Rust implementsDefault for various primitive types.
If you want to override a particular option, but still retain the other defaults:
§Derivable
This trait can be used with#[derive] if all of the type’s fields implementDefault. Whenderived, it will use the default value for each field’s type.
§enums
When using#[derive(Default)] on anenum, you need to choose which unit variant will bedefault. You do this by placing the#[default] attribute on the variant.
You cannot use the#[default] attribute on non-unit or non-exhaustive variants.
The#[default] attribute was stabilized in Rust 1.62.0.
§How can I implementDefault?
Provide an implementation for thedefault() method that returns the value ofyour type that should be the default:
§Examples
Required Methods§
1.0.0 ·Sourcefndefault() -> Self
fndefault() -> Self
Returns the “default value” for a type.
Default values are often some kind of initial value, identity value, or anything else thatmay make sense as a default.
§Examples
Using built-in default values:
leti: 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:
Dyn Compatibility§
This trait isnotdyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.
Implementors§
implDefault for &str
implDefault for &CStr
implDefault for &OsStr
implDefault for &mutstr
implDefault forAsciiChar
implDefault forbool
implDefault forchar
implDefault forf16
implDefault forf32
implDefault forf64
implDefault forf128
implDefault fori8
implDefault fori16
implDefault fori32
implDefault fori64
implDefault fori128
implDefault forisize
implDefault foru8
implDefault foru16
implDefault foru32
implDefault foru64
implDefault foru128
implDefault for()
implDefault forusize
implDefault forGlobal
implDefault forSystem
implDefault forBox<str>
implDefault forBox<CStr>
implDefault forBox<OsStr>
implDefault forByteString
implDefault forCString
implDefault forOsString
implDefault forError
implDefault forFormattingOptions
implDefault forFileTimes
implDefault forDefaultHasher
implDefault forRandomState
implDefault forSipHasher
implDefault for std::io::Empty
implDefault forSink
implDefault forPhantomPinned
implDefault forRangeFull
implDefault forPathBuf
implDefault forExitCode
The default value isExitCode::SUCCESS
implDefault forExitStatus
The default value is one which indicates successful completion.
implDefault forAlignment
ReturnsAlignment::MIN, which is valid for any type.
implDefault forDefaultRandomSource
implDefault forRc<str>
implDefault forRc<CStr>
implDefault forString
implDefault forAtomicBool
implDefault forAtomicI8
implDefault forAtomicI16
implDefault forAtomicI32
implDefault forAtomicI64
implDefault forAtomicIsize
implDefault forAtomicU8
implDefault forAtomicU16
implDefault forAtomicU32
implDefault forAtomicU64
implDefault forAtomicUsize
implDefault for std::sync::nonpoison::Condvar
implDefault forArc<str>
implDefault forArc<CStr>
implDefault for std::sync::Condvar
implDefault forDuration
impl<'a>Default for &'aByteStr
impl<'a>Default for &'a mutByteStr
impl<'a>Default forPhantomContravariantLifetime<'a>
impl<'a>Default forPhantomCovariantLifetime<'a>
impl<'a>Default forPhantomInvariantLifetime<'a>
impl<'a, K, V>Default for std::collections::btree_map::Iter<'a, K, V>where K: 'a, V: 'a,
impl<'a, K, V>Default for std::collections::btree_map::IterMut<'a, K, V>where K: 'a, V: 'a,
impl<A, B>Default forChain<A, B>
impl<B>Default forCow<'_, B>
impl<H>Default forBuildHasherDefault<H>
impl<I>Default forCloned<I>where I:Default,
impl<I>Default forCopied<I>where I:Default,
impl<I>Default forEnumerate<I>where I:Default,
impl<I>Default forFlatten<I>
impl<I>Default forFuse<I>where I:Default,
impl<I>Default forRev<I>where I:Default,
impl<Idx>Default for std::ops::Range<Idx>where Idx:Default,
impl<Idx>Default for std::range::Range<Idx>where Idx:Default,
impl<K>Default for std::collections::hash_set::IntoIter<K>
impl<K>Default for std::collections::hash_set::Iter<'_, K>
impl<K, V>Default for std::collections::btree_map::Keys<'_, K, V>
impl<K, V>Default for std::collections::btree_map::Range<'_, K, V>
impl<K, V>Default forRangeMut<'_, K, V>
impl<K, V>Default for std::collections::btree_map::Values<'_, K, V>
impl<K, V>Default for std::collections::btree_map::ValuesMut<'_, K, V>
impl<K, V>Default for std::collections::hash_map::IntoIter<K, V>
impl<K, V>Default for std::collections::hash_map::IntoKeys<K, V>
impl<K, V>Default for std::collections::hash_map::IntoValues<K, V>
impl<K, V>Default for std::collections::hash_map::Iter<'_, K, V>
impl<K, V>Default for std::collections::hash_map::IterMut<'_, K, V>
impl<K, V>Default for std::collections::hash_map::Keys<'_, K, V>
impl<K, V>Default for std::collections::hash_map::Values<'_, K, V>
impl<K, V>Default for std::collections::hash_map::ValuesMut<'_, K, V>
impl<K, V>Default forBTreeMap<K, V>
impl<K, V, A>Default for std::collections::btree_map::IntoIter<K, V, A>
impl<K, V, A>Default for std::collections::btree_map::IntoKeys<K, V, A>
impl<K, V, A>Default for std::collections::btree_map::IntoValues<K, V, A>
impl<K, V, S>Default forHashMap<K, V, S>where S:Default,
impl<T>Default for &[T]
impl<T>Default for &mut[T]
impl<T>Default forOption<T>
impl<T>Default for[T; 0]
impl<T>Default for[T; 1]where T:Default,
impl<T>Default for[T; 2]where T:Default,
impl<T>Default for[T; 3]where T:Default,
impl<T>Default for[T; 4]where T:Default,
impl<T>Default for[T; 5]where T:Default,
impl<T>Default for[T; 6]where T:Default,
impl<T>Default for[T; 7]where T:Default,
impl<T>Default for[T; 8]where T:Default,
impl<T>Default for[T; 9]where T:Default,
impl<T>Default for[T; 10]where T:Default,
impl<T>Default for[T; 11]where T:Default,
impl<T>Default for[T; 12]where T:Default,
impl<T>Default for[T; 13]where T:Default,
impl<T>Default for[T; 14]where T:Default,
impl<T>Default for[T; 15]where T:Default,
impl<T>Default for[T; 16]where T:Default,
impl<T>Default for[T; 17]where T:Default,
impl<T>Default for[T; 18]where T:Default,
impl<T>Default for[T; 19]where T:Default,
impl<T>Default for[T; 20]where T:Default,
impl<T>Default for[T; 21]where T:Default,
impl<T>Default for[T; 22]where T:Default,
impl<T>Default for[T; 23]where T:Default,
impl<T>Default for[T; 24]where T:Default,
impl<T>Default for[T; 25]where T:Default,
impl<T>Default for[T; 26]where T:Default,
impl<T>Default for[T; 27]where T:Default,
impl<T>Default for[T; 28]where T:Default,
impl<T>Default for[T; 29]where T:Default,
impl<T>Default for[T; 30]where T:Default,
impl<T>Default for[T; 31]where T:Default,
impl<T>Default for[T; 32]where T:Default,
impl<T>Default for*const T
impl<T>Default for*mut T
impl<T>Default for(T₁, T₂, …, Tₙ)where T:Default,
This trait is implemented for tuples up to twelve items long.