Movatterモバイル変換


[0]ホーム

URL:


Default

std::default

TraitDefault 

1.0.0 (const:unstable) ·Source
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:

structSomeOptions {    foo: i32,    bar: f32,}

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:

fnmain() {letoptions = SomeOptions { foo:42, ..Default::default() };}

§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.

#[derive(Default)]enumKind {#[default]A,    B,    C,}

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:

enumKind {    A,    B,    C,}implDefaultforKind {fndefault() ->Self{ Kind::A }}

§Examples

#[derive(Default)]structSomeOptions {    foo: i32,    bar: f32,}

Required Methods§

1.0.0 ·Source

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:

enumKind {    A,    B,    C,}implDefaultforKind {fndefault() ->Self{ Kind::A }}

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§

1.0.0 (const:unstable) ·Source§

implDefault for &str

1.10.0 ·Source§

implDefault for &CStr

1.9.0 ·Source§

implDefault for &OsStr

1.28.0 (const:unstable) ·Source§

implDefault for &mutstr

1.0.0 (const:unstable) ·Source§

implDefault forAsciiChar

1.0.0 (const:unstable) ·Source§

implDefault forbool

1.0.0 (const:unstable) ·Source§

implDefault forchar

1.0.0 (const:unstable) ·Source§

implDefault forf16

1.0.0 (const:unstable) ·Source§

implDefault forf32

1.0.0 (const:unstable) ·Source§

implDefault forf64

1.0.0 (const:unstable) ·Source§

implDefault forf128

1.0.0 (const:unstable) ·Source§

implDefault fori8

1.0.0 (const:unstable) ·Source§

implDefault fori16

1.0.0 (const:unstable) ·Source§

implDefault fori32

1.0.0 (const:unstable) ·Source§

implDefault fori64

1.0.0 (const:unstable) ·Source§

implDefault fori128

1.0.0 (const:unstable) ·Source§

implDefault forisize

1.0.0 (const:unstable) ·Source§

implDefault foru8

1.0.0 (const:unstable) ·Source§

implDefault foru16

1.0.0 (const:unstable) ·Source§

implDefault foru32

1.0.0 (const:unstable) ·Source§

implDefault foru64

1.0.0 (const:unstable) ·Source§

implDefault foru128

1.0.0 (const:unstable) ·Source§

implDefault for()

1.0.0 (const:unstable) ·Source§

implDefault forusize

Source§

implDefault forGlobal

1.28.0 ·Source§

implDefault forSystem

1.17.0 ·Source§

implDefault forBox<str>

1.17.0 ·Source§

implDefault forBox<CStr>

1.17.0 ·Source§

implDefault forBox<OsStr>

Source§

implDefault forByteString

1.10.0 ·Source§

implDefault forCString

1.9.0 ·Source§

implDefault forOsString

1.0.0 ·Source§

implDefault forError

Source§

implDefault forFormattingOptions

1.75.0 ·Source§

implDefault forFileTimes

1.13.0 ·Source§

implDefault forDefaultHasher

1.7.0 ·Source§

implDefault forRandomState

1.0.0 ·Source§

implDefault forSipHasher

1.0.0 ·Source§

implDefault for std::io::Empty

1.0.0 ·Source§

implDefault forSink

1.33.0 ·Source§

implDefault forPhantomPinned

1.0.0 ·Source§

implDefault forRangeFull

1.17.0 ·Source§

implDefault forPathBuf

1.75.0 ·Source§

implDefault forExitCode

The default value isExitCode::SUCCESS

1.73.0 ·Source§

implDefault forExitStatus

The default value is one which indicates successful completion.

Source§

implDefault forAlignment

ReturnsAlignment::MIN, which is valid for any type.

Source§

implDefault forDefaultRandomSource

1.80.0 ·Source§

implDefault forRc<str>

1.80.0 ·Source§

implDefault forRc<CStr>

1.0.0 (const:unstable) ·Source§

implDefault forString

1.0.0 ·Source§

implDefault forAtomicBool

1.34.0 ·Source§

implDefault forAtomicI8

1.34.0 ·Source§

implDefault forAtomicI16

1.34.0 ·Source§

implDefault forAtomicI32

1.34.0 ·Source§

implDefault forAtomicI64

1.0.0 ·Source§

implDefault forAtomicIsize

1.34.0 ·Source§

implDefault forAtomicU8

1.34.0 ·Source§

implDefault forAtomicU16

1.34.0 ·Source§

implDefault forAtomicU32

1.34.0 ·Source§

implDefault forAtomicU64

1.0.0 ·Source§

implDefault forAtomicUsize

Source§

implDefault for std::sync::nonpoison::Condvar

1.80.0 ·Source§

implDefault forArc<str>

1.80.0 ·Source§

implDefault forArc<CStr>

1.10.0 ·Source§

implDefault for std::sync::Condvar

1.3.0 ·Source§

implDefault forDuration

Source§

impl<'a>Default for &'aByteStr

Source§

impl<'a>Default for &'a mutByteStr

Source§

impl<'a>Default forPhantomContravariantLifetime<'a>

Source§

impl<'a>Default forPhantomCovariantLifetime<'a>

Source§

impl<'a>Default forPhantomInvariantLifetime<'a>

1.70.0 ·Source§

impl<'a, K, V>Default for std::collections::btree_map::Iter<'a, K, V>
where K: 'a, V: 'a,

1.70.0 ·Source§

impl<'a, K, V>Default for std::collections::btree_map::IterMut<'a, K, V>
where K: 'a, V: 'a,

1.70.0 ·Source§

impl<A, B>Default forChain<A, B>
where A:Default, B:Default,

1.11.0 ·Source§

impl<B>Default forCow<'_, B>
where B:ToOwned + ?Sized, <B asToOwned>::Owned:Default,

1.7.0 ·Source§

impl<H>Default forBuildHasherDefault<H>

1.70.0 ·Source§

impl<I>Default forCloned<I>
where I:Default,

1.70.0 ·Source§

impl<I>Default forCopied<I>
where I:Default,

1.70.0 ·Source§

impl<I>Default forEnumerate<I>
where I:Default,

1.70.0 ·Source§

impl<I>Default forFlatten<I>

1.70.0 ·Source§

impl<I>Default forFuse<I>
where I:Default,

1.70.0 ·Source§

impl<I>Default forRev<I>
where I:Default,

1.0.0 ·Source§

impl<Idx>Default for std::ops::Range<Idx>
where Idx:Default,

Source§

impl<Idx>Default for std::range::Range<Idx>
where Idx:Default,

1.83.0 ·Source§

impl<K>Default for std::collections::hash_set::IntoIter<K>

1.83.0 ·Source§

impl<K>Default for std::collections::hash_set::Iter<'_, K>

1.70.0 ·Source§

impl<K, V>Default for std::collections::btree_map::Keys<'_, K, V>

1.70.0 ·Source§

impl<K, V>Default for std::collections::btree_map::Range<'_, K, V>

1.82.0 ·Source§

impl<K, V>Default forRangeMut<'_, K, V>

1.70.0 ·Source§

impl<K, V>Default for std::collections::btree_map::Values<'_, K, V>

1.82.0 ·Source§

impl<K, V>Default for std::collections::btree_map::ValuesMut<'_, K, V>

1.83.0 ·Source§

impl<K, V>Default for std::collections::hash_map::IntoIter<K, V>

1.83.0 ·Source§

impl<K, V>Default for std::collections::hash_map::IntoKeys<K, V>

1.83.0 ·Source§

impl<K, V>Default for std::collections::hash_map::IntoValues<K, V>

1.83.0 ·Source§

impl<K, V>Default for std::collections::hash_map::Iter<'_, K, V>

1.83.0 ·Source§

impl<K, V>Default for std::collections::hash_map::IterMut<'_, K, V>

1.83.0 ·Source§

impl<K, V>Default for std::collections::hash_map::Keys<'_, K, V>

1.83.0 ·Source§

impl<K, V>Default for std::collections::hash_map::Values<'_, K, V>

1.83.0 ·Source§

impl<K, V>Default for std::collections::hash_map::ValuesMut<'_, K, V>

1.0.0 ·Source§

impl<K, V>Default forBTreeMap<K, V>

1.70.0 ·Source§

impl<K, V, A>Default for std::collections::btree_map::IntoIter<K, V, A>

1.70.0 ·Source§

impl<K, V, A>Default for std::collections::btree_map::IntoKeys<K, V, A>

1.70.0 ·Source§

impl<K, V, A>Default for std::collections::btree_map::IntoValues<K, V, A>

1.0.0 ·Source§

impl<K, V, S>Default forHashMap<K, V, S>
where S:Default,

1.0.0 (const:unstable) ·Source§

impl<T>Default for &[T]

1.5.0 (const:unstable) ·Source§

impl<T>Default for &mut[T]

1.0.0 (const:unstable) ·Source§

impl<T>Default forOption<T>

1.4.0 ·Source§

impl<T>Default for[T; 0]

1.4.0 ·Source§

impl<T>Default for[T; 1]
where T:Default,

1.4.0 ·Source§

impl<T>Default for[T; 2]
where T:Default,

1.4.0 ·Source§

impl<T>Default for[T; 3]
where T:Default,

1.4.0 ·Source§

impl<T>Default for[T; 4]
where T:Default,

1.4.0 ·Source§

impl<T>Default for[T; 5]
where T:Default,

1.4.0 ·Source§

impl<T>Default for[T; 6]
where T:Default,

1.4.0 ·Source§

impl<T>Default for[T; 7]
where T:Default,

1.4.0 ·Source§

impl<T>Default for[T; 8]
where T:Default,

1.4.0 ·Source§

impl<T>Default for[T; 9]
where T:Default,

1.4.0 ·Source§

impl<T>Default for[T; 10]
where T:Default,

1.4.0 ·Source§

impl<T>Default for[T; 11]
where T:Default,

1.4.0 ·Source§

impl<T>Default for[T; 12]
where T:Default,

1.4.0 ·Source§

impl<T>Default for[T; 13]
where T:Default,

1.4.0 ·Source§

impl<T>Default for[T; 14]
where T:Default,

1.4.0 ·Source§

impl<T>Default for[T; 15]
where T:Default,

1.4.0 ·Source§

impl<T>Default for[T; 16]
where T:Default,

1.4.0 ·Source§

impl<T>Default for[T; 17]
where T:Default,

1.4.0 ·Source§

impl<T>Default for[T; 18]
where T:Default,

1.4.0 ·Source§

impl<T>Default for[T; 19]
where T:Default,

1.4.0 ·Source§

impl<T>Default for[T; 20]
where T:Default,

1.4.0 ·Source§

impl<T>Default for[T; 21]
where T:Default,

1.4.0 ·Source§

impl<T>Default for[T; 22]
where T:Default,

1.4.0 ·Source§

impl<T>Default for[T; 23]
where T:Default,

1.4.0 ·Source§

impl<T>Default for[T; 24]
where T:Default,

1.4.0 ·Source§

impl<T>Default for[T; 25]
where T:Default,

1.4.0 ·Source§

impl<T>Default for[T; 26]
where T:Default,

1.4.0 ·Source§

impl<T>Default for[T; 27]
where T:Default,

1.4.0 ·Source§

impl<T>Default for[T; 28]
where T:Default,

1.4.0 ·Source§

impl<T>Default for[T; 29]
where T:Default,

1.4.0 ·Source§

impl<T>Default for[T; 30]
where T:Default,

1.4.0 ·Source§

impl<T>Default for[T; 31]
where T:Default,

1.4.0 ·Source§

impl<T>Default for[T; 32]
where T:Default,

1.88.0 ·Source§

impl<T>Default for*const T
where T:Thin + ?Sized,

1.88.0 ·Source§

impl<T>Default for*mut T
where T:Thin + ?Sized,

1.0.0 ·Source§

impl<T>Default for(T₁, T₂, …, Tₙ)
where T:Default,

This trait is implemented for tuples up to twelve items long.

1.0.0 ·Source§

impl<T>Default forBox<[T]>

1.0.0 ·Source§

impl<T>Default forBox<T>
where T:Default,

1.0.0 (const:unstable) ·Source§

impl<T>Default forCell<T>
where T:Default,

1.80.0 ·Source§

impl<T>Default forLazyCell<T>
where T:Default,

1.70.0 ·Source§

impl<T>Default forOnceCell<T>

1.0.0 (const:unstable) ·Source§

impl<T>Default forRefCell<T>
where T:Default,

Source§

impl<T>Default forSyncUnsafeCell<T>
where T:Default,

1.10.0 (const:unstable) ·Source§

impl<T>Default forUnsafeCell<T>
where T:Default,

1.19.0 (const:unstable) ·Source§

impl<T>Default forReverse<T>
where T:Default,

1.70.0 ·Source§

impl<T>Default for std::collections::binary_heap::IntoIter<T>

1.82.0 ·Source§

impl<T>Default for std::collections::binary_heap::Iter<'_, T>

1.70.0 ·Source§

impl<T>Default for std::collections::btree_set::Iter<'_, T>

1.70.0 ·Source§

impl<T>Default for std::collections::btree_set::Range<'_, T>

1.70.0 ·Source§

impl<T>Default for std::collections::linked_list::IntoIter<T>

1.70.0 ·Source§

impl<T>Default for std::collections::linked_list::Iter<'_, T>

1.70.0 ·Source§

impl<T>Default for std::collections::linked_list::IterMut<'_, T>

1.0.0 ·Source§

impl<T>Default forBTreeSet<T>

1.0.0 ·Source§

impl<T>Default forBinaryHeap<T>
where T:Ord,

1.0.0 ·Source§

impl<T>Default forLinkedList<T>

1.0.0 ·Source§

impl<T>Default forVecDeque<T>

1.82.0 ·Source§

impl<T>Default for std::collections::vec_deque::Iter<'_, T>

1.82.0 ·Source§

impl<T>Default for std::collections::vec_deque::IterMut<'_, T>

1.2.0 (const:unstable) ·Source§

impl<T>Default for std::iter::Empty<T>

Source§

impl<T>Default forPhantomContravariant<T>
where T: ?Sized,

Source§

impl<T>Default forPhantomCovariant<T>
where T: ?Sized,

1.0.0 (const:unstable) ·Source§

impl<T>Default forPhantomData<T>
where T: ?Sized,

Source§

impl<T>Default forPhantomInvariant<T>
where T: ?Sized,

1.20.0 ·Source§

impl<T>Default forManuallyDrop<T>
where T:Default + ?Sized,

1.74.0 ·Source§

impl<T>Default forSaturating<T>
where T:Default,

1.0.0 ·Source§

impl<T>Default forWrapping<T>
where T:Default,

1.62.0 ·Source§

impl<T>Default forAssertUnwindSafe<T>
where T:Default,

1.91.0 ·Source§

impl<T>Default forPin<Box<T>>
whereBox<T>:Default, T: ?Sized,

1.91.0 ·Source§

impl<T>Default forPin<Rc<T>>
whereRc<T>:Default, T: ?Sized,

1.91.0 ·Source§

impl<T>Default forPin<Arc<T>>
whereArc<T>:Default, T: ?Sized,

Source§

impl<T>Default forUnsafePinned<T>
where T:Default,

1.80.0 ·Source§

impl<T>Default forRc<[T]>

1.0.0 ·Source§

impl<T>Default forRc<T>
where T:Default,

1.10.0 ·Source§

impl<T>Default for std::rc::Weak<T>

1.70.0 ·Source§

impl<T>Default for std::slice::Iter<'_, T>

1.70.0 ·Source§

impl<T>Default for std::slice::IterMut<'_, T>

1.0.0 ·Source§

impl<T>Default forAtomicPtr<T>

1.80.0 ·Source§

impl<T>Default forArc<[T]>

1.0.0 ·Source§

impl<T>Default forArc<T>
where T:Default,

Source§

impl<T>Default forExclusive<T>
where T:Default + ?Sized,

1.70.0 ·Source§

impl<T>Default forOnceLock<T>

1.10.0 ·Source§

impl<T>Default for std::sync::Weak<T>

1.0.0 (const:unstable) ·Source§

impl<T>Default forVec<T>

1.70.0 ·Source§

impl<T, A>Default for std::collections::btree_set::IntoIter<T, A>

1.70.0 ·Source§

impl<T, A>Default for std::vec::IntoIter<T, A>
where A:Allocator +Default,

1.0.0 ·Source§

impl<T, S>Default forHashSet<T, S>
where S:Default,

1.89.0 ·Source§

impl<T, const N:usize>Default for std::array::IntoIter<T, N>

Source§

impl<T, const N:usize>Default forMask<T, N>

Source§

impl<T, const N:usize>Default forSimd<T, N>

1.0.0 ·Source§

impl<T:Default>Default forCursor<T>

Source§

impl<T:Default>Default for std::sync::nonpoison::RwLock<T>

1.80.0 ·Source§

impl<T:Default>Default forLazyLock<T>

Source§

impl<T:Default>Default forReentrantLock<T>

1.10.0 ·Source§

impl<T:Default>Default for std::sync::RwLock<T>

Source§

impl<T: ?Sized +Default>Default for std::sync::nonpoison::Mutex<T>

1.10.0 ·Source§

impl<T: ?Sized +Default>Default for std::sync::Mutex<T>


[8]ページ先頭

©2009-2025 Movatter.jp