Trait nom::lib::std::prelude::v1::rust_2018::Default 1.0.0[−][src]
pub trait Default { pub fn default() -> Self; }
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
pub fn default() -> Self
[src]
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
impl<T> Default for Mutex<T> where
T: Default + ?Sized,
[src]
impl<T> Default for Mutex<T> where
T: Default + ?Sized,
[src]impl<T> Default for RwLock<T> where
T: Default,
[src]
impl<T> Default for RwLock<T> where
T: Default,
[src]impl Default for Condvar
[src]
impl Default for Condvar
[src]impl<T> Default for SyncLazy<T, fn() -> T> where
T: Default,
[src]
impl<T> Default for SyncLazy<T, fn() -> T> where
T: Default,
[src]impl<T> Default for SyncOnceCell<T>
[src]
impl<T> Default for SyncOnceCell<T>
[src]pub fn default() -> SyncOnceCell<T>
[src]
impl<'_> Default for &'_ mut str
[src]
impl<'_> Default for &'_ mut str
[src]impl<T> Default for Lazy<T, fn() -> T> where
T: Default,
[src]
impl<T> Default for Lazy<T, fn() -> T> where
T: Default,
[src]impl Default for AtomicUsize
[src]
impl Default for AtomicUsize
[src]pub fn default() -> AtomicUsize
[src]
impl<A, B, C> Default for (A, B, C) where
C: Default,
A: Default,
B: Default,
[src]
impl<A, B, C> Default for (A, B, C) where
C: Default,
A: Default,
B: Default,
[src]impl<A, B, C, D> Default for (A, B, C, D) where
C: Default,
A: Default,
B: Default,
D: Default,
[src]
impl<A, B, C, D> Default for (A, B, C, D) where
C: Default,
A: Default,
B: Default,
D: Default,
[src]impl<A, B, C, D, E, F, G> Default for (A, B, C, D, E, F, G) where
C: Default,
F: Default,
E: Default,
G: Default,
A: Default,
B: Default,
D: Default,
[src]
impl<A, B, C, D, E, F, G> Default for (A, B, C, D, E, F, G) where
C: Default,
F: Default,
E: Default,
G: Default,
A: Default,
B: Default,
D: Default,
[src]impl<T> Default for PhantomData<T> where
T: ?Sized,
[src]
impl<T> Default for PhantomData<T> where
T: ?Sized,
[src]pub fn default() -> PhantomData<T>
[src]
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
C: Default,
F: Default,
E: Default,
I: Default,
G: Default,
H: Default,
A: Default,
B: Default,
D: Default,
J: Default,
K: Default,
[src]
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
C: Default,
F: Default,
E: Default,
I: Default,
G: Default,
H: Default,
A: Default,
B: Default,
D: Default,
J: Default,
K: Default,
[src]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
C: Default,
F: Default,
E: Default,
I: Default,
G: Default,
H: Default,
A: Default,
B: Default,
D: Default,
J: Default,
K: Default,
L: Default,
[src]
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
C: Default,
F: Default,
E: Default,
I: Default,
G: Default,
H: Default,
A: Default,
B: Default,
D: Default,
J: Default,
K: Default,
L: Default,
[src]impl<A, B, C, D, E, F, G, H, I> Default for (A, B, C, D, E, F, G, H, I) where
C: Default,
F: Default,
E: Default,
I: Default,
G: Default,
H: Default,
A: Default,
B: Default,
D: Default,
[src]
impl<A, B, C, D, E, F, G, H, I> Default for (A, B, C, D, E, F, G, H, I) where
C: Default,
F: Default,
E: Default,
I: Default,
G: Default,
H: Default,
A: Default,
B: Default,
D: Default,
[src]impl<A, B, C, D, E, F, G, H> Default for (A, B, C, D, E, F, G, H) where
C: Default,
F: Default,
E: Default,
G: Default,
H: Default,
A: Default,
B: Default,
D: Default,
[src]
impl<A, B, C, D, E, F, G, H> Default for (A, B, C, D, E, F, G, H) where
C: Default,
F: Default,
E: Default,
G: Default,
H: Default,
A: Default,
B: Default,
D: Default,
[src]impl<T> Default for AtomicPtr<T>
[src]
impl<T> Default for AtomicPtr<T>
[src]impl Default for AtomicIsize
[src]
impl Default for AtomicIsize
[src]pub fn default() -> AtomicIsize
[src]
impl<'_, T> Default for &'_ [T]
[src]
impl<'_, T> Default for &'_ [T]
[src]impl Default for AtomicBool
[src]
impl Default for AtomicBool
[src]pub fn default() -> AtomicBool
[src]
Creates an AtomicBool
initialized to false
.
impl<A, B, C, D, E> Default for (A, B, C, D, E) where
C: Default,
E: Default,
A: Default,
B: Default,
D: Default,
[src]
impl<A, B, C, D, E> Default for (A, B, C, D, E) where
C: Default,
E: Default,
A: Default,
B: Default,
D: Default,
[src]impl<'_, T> Default for &'_ mut [T]
[src]
impl<'_, T> Default for &'_ mut [T]
[src]impl<A, B, C, D, E, F, G, H, I, J> Default for (A, B, C, D, E, F, G, H, I, J) where
C: Default,
F: Default,
E: Default,
I: Default,
G: Default,
H: Default,
A: Default,
B: Default,
D: Default,
J: Default,
[src]
impl<A, B, C, D, E, F, G, H, I, J> Default for (A, B, C, D, E, F, G, H, I, J) where
C: Default,
F: Default,
E: Default,
I: Default,
G: Default,
H: Default,
A: Default,
B: Default,
D: Default,
J: Default,
[src]impl Default for PhantomPinned
[src]
impl Default for PhantomPinned
[src]pub fn default() -> PhantomPinned
[src]
impl<A, B, C, D, E, F> Default for (A, B, C, D, E, F) where
C: Default,
F: Default,
E: Default,
A: Default,
B: Default,
D: Default,
[src]
impl<A, B, C, D, E, F> Default for (A, B, C, D, E, F) where
C: Default,
F: Default,
E: Default,
A: Default,
B: Default,
D: Default,
[src]impl<T> Default for Cell<T> where
T: Default,
[src]
impl<T> Default for Cell<T> where
T: Default,
[src]impl<T> Default for UnsafeCell<T> where
T: Default,
[src]
impl<T> Default for UnsafeCell<T> where
T: Default,
[src]pub fn default() -> UnsafeCell<T>
[src]
Creates an UnsafeCell
, with the Default
value for T.
impl<T> Default for RefCell<T> where
T: Default,
[src]
impl<T> Default for RefCell<T> where
T: Default,
[src]impl<T> Default for Weak<T>
[src]
impl<T> Default for Weak<T>
[src]impl<T> Default for Arc<T> where
T: Default,
[src]
impl<T> Default for Arc<T> where
T: Default,
[src]impl<T> Default for Rc<T> where
T: Default,
[src]
impl<T> Default for Rc<T> where
T: Default,
[src]impl<T> Default for Weak<T>
[src]
impl<T> Default for Weak<T>
[src]Implementors
impl Default for Box<str, Global>
1.17.0[src]
impl Default for Box<str, Global>
1.17.0[src]pub fn default() -> Box<str, Global>ⓘNotable traits for Box<W, Global>
impl<W> Write for Box<W, Global> where
W: Write + ?Sized, impl<R> Read for Box<R, Global> where
R: Read + ?Sized, 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;
[src]
Notable traits for Box<W, Global>
impl<W> Write for Box<W, Global> where
W: Write + ?Sized, impl<R> Read for Box<R, Global> where
R: Read + ?Sized, 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;
impl Default for Box<CStr, Global>
1.17.0[src]
impl Default for Box<CStr, Global>
1.17.0[src]pub fn default() -> Box<CStr, Global>ⓘNotable traits for Box<W, Global>
impl<W> Write for Box<W, Global> where
W: Write + ?Sized, impl<R> Read for Box<R, Global> where
R: Read + ?Sized, 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;
[src]
Notable traits for Box<W, Global>
impl<W> Write for Box<W, Global> where
W: Write + ?Sized, impl<R> Read for Box<R, Global> where
R: Read + ?Sized, 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;
impl Default for Box<OsStr, Global>
1.17.0[src]
impl Default for Box<OsStr, Global>
1.17.0[src]pub fn default() -> Box<OsStr, Global>ⓘNotable traits for Box<W, Global>
impl<W> Write for Box<W, Global> where
W: Write + ?Sized, impl<R> Read for Box<R, Global> where
R: Read + ?Sized, 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;
[src]
Notable traits for Box<W, Global>
impl<W> Write for Box<W, Global> where
W: Write + ?Sized, impl<R> Read for Box<R, Global> where
R: Read + ?Sized, 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;
impl Default for DefaultHasher
1.13.0[src]
impl Default for DefaultHasher
1.13.0[src]pub fn default() -> DefaultHasher
[src]
Creates a new DefaultHasher
using new
.
See its documentation for more.
impl Default for RandomState
1.7.0[src]
impl Default for RandomState
1.7.0[src]pub fn default() -> RandomState
[src]
Constructs a new RandomState
.
impl<'_, B> Default for Cow<'_, B> where
B: ToOwned + ?Sized,
<B as ToOwned>::Owned: Default,
1.11.0[src]
impl<'_, B> Default for Cow<'_, B> where
B: ToOwned + ?Sized,
<B as ToOwned>::Owned: Default,
1.11.0[src]impl<H> Default for BuildHasherDefault<H>
1.7.0[src]
impl<H> Default for BuildHasherDefault<H>
1.7.0[src]pub fn default() -> BuildHasherDefault<H>
[src]
impl<T> Default for Box<[T], Global>
[src]
impl<T> Default for Box<[T], Global>
[src]pub fn default() -> Box<[T], Global>ⓘNotable traits for Box<W, Global>
impl<W> Write for Box<W, Global> where
W: Write + ?Sized, impl<R> Read for Box<R, Global> where
R: Read + ?Sized, 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;
[src]
Notable traits for Box<W, Global>
impl<W> Write for Box<W, Global> where
W: Write + ?Sized, impl<R> Read for Box<R, Global> where
R: Read + ?Sized, 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;
impl<T> Default for Box<T, Global> where
T: Default,
[src]
impl<T> Default for Box<T, Global> where
T: Default,
[src]pub fn default() -> Box<T, Global>ⓘNotable traits for Box<W, Global>
impl<W> Write for Box<W, Global> where
W: Write + ?Sized, impl<R> Read for Box<R, Global> where
R: Read + ?Sized, 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;
[src]
Notable traits for Box<W, Global>
impl<W> Write for Box<W, Global> where
W: Write + ?Sized, impl<R> Read for Box<R, Global> where
R: Read + ?Sized, 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;
Creates a Box<T>
, with the Default
value for T.
impl<T> Default for BinaryHeap<T> where
T: Ord,
[src]
impl<T> Default for BinaryHeap<T> where
T: Ord,
[src]pub fn default() -> BinaryHeap<T>
[src]
Creates an empty BinaryHeap<T>
.
impl<T> Default for LinkedList<T>
[src]
impl<T> Default for LinkedList<T>
[src]pub fn default() -> LinkedList<T>
[src]
Creates an empty LinkedList<T>
.
impl<T> Default for ManuallyDrop<T> where
T: Default + ?Sized,
1.20.0[src]
impl<T> Default for ManuallyDrop<T> where
T: Default + ?Sized,
1.20.0[src]