Trait wasmtime_environ::__core::prelude::v1::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
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
Constructs a new RandomState
.
Creates a new empty cell.
Example
#![feature(once_cell)]
use std::lazy::SyncOnceCell;
fn main() {
assert_eq!(SyncOnceCell::<()>::new(), SyncOnceCell::default());
}
Creates a new DefaultHasher
using new
.
See its documentation for more.
Creates an empty BinaryHeap<T>
.
Creates an empty LinkedList<T>
.
impl<'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>,
impl<'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,
pub fn default() -> RawTable<T, A>
pub fn default() -> HashMap<K, V, S, A>
pub fn default() -> HashMap<K, V, S, A>
Creates an empty HashMap<K, V, S, A>
, with the Default
value for the hasher and allocator.
pub fn default() -> HashSet<T, S, A>
pub fn default() -> HashSet<T, S, A>
Creates an empty HashSet<T, S>
with the Default
value for the hasher.
impl<'input, Endian> Default for EndianSlice<'input, Endian> where
Endian: Default + Endianity,
impl<'input, Endian> Default for EndianSlice<'input, Endian> where
Endian: Default + Endianity,
Implementors
Create an empty list.