Trait ockam_core::lib::Default 1.0.0[−][src]
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
fn default() -> Self
[src]
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 Default for AtomicBool
[src]
impl Default for AtomicBool
[src]pub fn default() -> AtomicBool
[src]
pub fn default() -> AtomicBool
[src]Creates an AtomicBool
initialized to false
.
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,
A: Default,
E: Default,
B: Default,
F: Default,
I: Default,
H: Default,
D: Default,
G: 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,
A: Default,
E: Default,
B: Default,
F: Default,
I: Default,
H: Default,
D: Default,
G: Default,
J: Default,
[src]impl<'_, T> Default for &'_ mut [T]
[src]
impl<'_, T> Default for &'_ mut [T]
[src]impl<A, B, C, D, E, F, G> Default for (A, B, C, D, E, F, G) where
C: Default,
A: Default,
E: Default,
B: Default,
F: Default,
D: Default,
G: Default,
[src]
impl<A, B, C, D, E, F, G> Default for (A, B, C, D, E, F, G) where
C: Default,
A: Default,
E: Default,
B: Default,
F: Default,
D: Default,
G: 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]
pub fn default() -> UnsafeCell<T>
[src]Creates an UnsafeCell
, with the Default
value for T.
impl<A, B, C, D, E, F, G, H> Default for (A, B, C, D, E, F, G, H) where
C: Default,
A: Default,
E: Default,
B: Default,
F: Default,
H: Default,
D: Default,
G: Default,
[src]
impl<A, B, C, D, E, F, G, H> Default for (A, B, C, D, E, F, G, H) where
C: Default,
A: Default,
E: Default,
B: Default,
F: Default,
H: Default,
D: Default,
G: Default,
[src]impl<A, B, C, D, E> Default for (A, B, C, D, E) where
C: Default,
A: Default,
E: Default,
B: Default,
D: Default,
[src]
impl<A, B, C, D, E> Default for (A, B, C, D, E) where
C: Default,
A: Default,
E: 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,
A: Default,
E: Default,
B: Default,
F: Default,
I: Default,
H: Default,
D: Default,
G: 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,
A: Default,
E: Default,
B: Default,
F: Default,
I: Default,
H: Default,
D: Default,
G: 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,
A: Default,
E: Default,
K: Default,
B: Default,
F: Default,
I: Default,
H: Default,
D: Default,
G: Default,
J: 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,
A: Default,
E: Default,
K: Default,
B: Default,
F: Default,
I: Default,
H: Default,
D: Default,
G: Default,
J: Default,
L: 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, H, I, J, K> Default for (A, B, C, D, E, F, G, H, I, J, K) where
C: Default,
A: Default,
E: Default,
K: Default,
B: Default,
F: Default,
I: Default,
H: Default,
D: Default,
G: Default,
J: 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,
A: Default,
E: Default,
K: Default,
B: Default,
F: Default,
I: Default,
H: Default,
D: Default,
G: Default,
J: Default,
[src]impl Default for AtomicUsize
[src]
impl Default for AtomicUsize
[src]pub fn default() -> AtomicUsize
[src]
impl<A, B, C, D, E, F> Default for (A, B, C, D, E, F) where
C: Default,
A: Default,
E: Default,
B: Default,
F: Default,
D: Default,
[src]
impl<A, B, C, D, E, F> Default for (A, B, C, D, E, F) where
C: Default,
A: Default,
E: Default,
B: Default,
F: Default,
D: Default,
[src]impl Default for AtomicIsize
[src]
impl Default for AtomicIsize
[src]pub fn default() -> AtomicIsize
[src]
impl<H> Default for BuildHasherDefault<H>
[src]
impl<H> Default for BuildHasherDefault<H>
[src]pub fn default() -> BuildHasherDefault<H>
[src]
impl Default for RandomState
[src]
impl Default for RandomState
[src]pub fn default() -> RandomState
[src]
pub fn default() -> RandomState
[src]Constructs a new RandomState
.
impl<T> Default for SyncOnceCell<T>
[src]
impl<T> Default for SyncOnceCell<T>
[src]pub fn default() -> SyncOnceCell<T>
[src]
impl Default for DefaultHasher
[src]
impl Default for DefaultHasher
[src]pub fn default() -> DefaultHasher
[src]
pub fn default() -> DefaultHasher
[src]Creates a new DefaultHasher
using new
.
See its documentation for more.
impl Default for IgnoredAny
[src]
impl Default for IgnoredAny
[src]pub fn default() -> IgnoredAny
[src]
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);
pub fn default() -> AHasher
pub 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());
Implementors
impl Default for PhantomPinned
1.33.0[src]
impl Default for PhantomPinned
1.33.0[src]pub fn default() -> PhantomPinned
[src]
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<F, A> Future for Box<F, A> where
A: Allocator + 'static,
F: Future + Unpin + ?Sized, type Output = <F as Future>::Output;impl<I, A> Iterator for Box<I, A> where
A: Allocator,
I: Iterator + ?Sized, type Item = <I as Iterator>::Item;
[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<F, A> Future for Box<F, A> where
A: Allocator + 'static,
F: Future + Unpin + ?Sized, type Output = <F as Future>::Output;impl<I, A> Iterator for Box<I, A> where
A: Allocator,
I: Iterator + ?Sized, type Item = <I as Iterator>::Item;
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<F, A> Future for Box<F, A> where
A: Allocator + 'static,
F: Future + Unpin + ?Sized, type Output = <F as Future>::Output;impl<I, A> Iterator for Box<I, A> where
A: Allocator,
I: Iterator + ?Sized, type Item = <I as Iterator>::Item;
[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<F, A> Future for Box<F, A> where
A: Allocator + 'static,
F: Future + Unpin + ?Sized, type Output = <F as Future>::Output;impl<I, A> Iterator for Box<I, A> where
A: Allocator,
I: Iterator + ?Sized, type Item = <I as Iterator>::Item;
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<F, A> Future for Box<F, A> where
A: Allocator + 'static,
F: Future + Unpin + ?Sized, type Output = <F as Future>::Output;impl<I, A> Iterator for Box<I, A> where
A: Allocator,
I: Iterator + ?Sized, type Item = <I as Iterator>::Item;
[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<F, A> Future for Box<F, A> where
A: Allocator + 'static,
F: Future + Unpin + ?Sized, type Output = <F as Future>::Output;impl<I, A> Iterator for Box<I, A> where
A: Allocator,
I: Iterator + ?Sized, type Item = <I as Iterator>::Item;
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<K, V, S, A> Default for ockam_core::lib::HashMap<K, V, S, A> where
S: Default,
A: Default + Allocator + Clone,
[src]
impl<K, V, S, A> Default for ockam_core::lib::HashMap<K, V, S, A> where
S: Default,
A: Default + Allocator + Clone,
[src]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]pub fn default() -> ManuallyDrop<T>
[src]
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]
pub fn default() -> BinaryHeap<T>
[src]Creates an empty BinaryHeap<T>
.
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<F, A> Future for Box<F, A> where
A: Allocator + 'static,
F: Future + Unpin + ?Sized, type Output = <F as Future>::Output;impl<I, A> Iterator for Box<I, A> where
A: Allocator,
I: Iterator + ?Sized, type Item = <I as Iterator>::Item;
[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<F, A> Future for Box<F, A> where
A: Allocator + 'static,
F: Future + Unpin + ?Sized, type Output = <F as Future>::Output;impl<I, A> Iterator for Box<I, A> where
A: Allocator,
I: Iterator + ?Sized, type Item = <I as Iterator>::Item;
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<F, A> Future for Box<F, A> where
A: Allocator + 'static,
F: Future + Unpin + ?Sized, type Output = <F as Future>::Output;impl<I, A> Iterator for Box<I, A> where
A: Allocator,
I: Iterator + ?Sized, type Item = <I as Iterator>::Item;
[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<F, A> Future for Box<F, A> where
A: Allocator + 'static,
F: Future + Unpin + ?Sized, type Output = <F as Future>::Output;impl<I, A> Iterator for Box<I, A> where
A: Allocator,
I: Iterator + ?Sized, type Item = <I as Iterator>::Item;
[src]Creates a Box<T>
, with the Default
value for T.
impl<T> Default for LinkedList<T>
[src]
impl<T> Default for LinkedList<T>
[src]pub fn default() -> LinkedList<T>
[src]
pub fn default() -> LinkedList<T>
[src]Creates an empty LinkedList<T>
.
impl<T> Default for PhantomData<T> where
T: ?Sized,
[src]
impl<T> Default for PhantomData<T> where
T: ?Sized,
[src]