Trait nom::lib::std::prelude::v1::rust_2018::Default1.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 structs 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 derived, 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

Creates an empty OsStr.

Creates a new lazy value using Default as the initializing function.

Creates a new empty cell.

Example
#![feature(once_cell)]

use std::lazy::SyncOnceCell;

fn main() {
    assert_eq!(SyncOnceCell::<()>::new(), SyncOnceCell::default());
}

Creates a new RwLock<T>, with the Default value for T.

Constructs an empty OsString.

Creates a Condvar which is ready to be waited on and notified.

Creates an empty CString.

Creates a Mutex<T>, with the Default value for T.

Creates an empty slice.

Creates an empty str

Creates a mutable empty slice.

Returns the default value of 0

Returns the default value of 0

Returns the default value of 0

Creates an AtomicBool initialized to false.

Creates a RefCell<T>, with the Default value for T.

Returns the default value of 0

Returns the default value of \x00

Returns the default value of 0

Returns the default value of 0

Returns the default value of false

Returns the default value of 0

Returns the default value of ()

Creates a null AtomicPtr<T>.

Returns the default value of 0

Returns the default value of 0

Returns the default value of 0

Creates an UnsafeCell, with the Default value for T.

Returns the default value of 0

Returns the default value of 0.0

Returns the default value of 0.0

Returns the default value of 0

Creates an empty mutable str

Creates a Cell<T>, with the Default value for T.

Creates a new lazy value using Default as the initializing function.

Constructs a new Weak<T>, without allocating memory. Calling upgrade on the return value always gives None.

Examples
use std::sync::Weak;

let empty: Weak<i64> = Default::default();
assert!(empty.upgrade().is_none());

Constructs a new Weak<T>, without allocating any memory. Calling upgrade on the return value always gives None.

Examples
use std::rc::Weak;

let empty: Weak<i64> = Default::default();
assert!(empty.upgrade().is_none());

Creates a new Rc<T>, with the Default value for T.

Examples
use std::rc::Rc;

let x: Rc<i32> = Default::default();
assert_eq!(*x, 0);

Creates a new Arc<T>, with the Default value for T.

Examples
use std::sync::Arc;

let x: Arc<i32> = Default::default();
assert_eq!(*x, 0);

Return an empty array

Return an empty ArrayString

Implementors

impl Default for Adler32

impl Default for Config

impl Default for Builder

impl<A> Default for ArrayString<A> where
    A: Array<Item = u8> + Copy

impl<A: Array> Default for ArrayVec<A>

impl Default for Parsed

impl<'help> Default for App<'help>

impl<'help> Default for PossibleValue<'help>

impl<'help> Default for Arg<'help>

impl<'help> Default for ArgGroup<'help>

impl<'a> Default for Values<'a>

impl Default for OsValues<'_>

impl<'a> Default for Indices<'a>

impl Default for Theme

impl Default for Config

impl Default for Value

impl<'a> Default for Select<'a>

impl<T> Default for Injector<T>

impl<T: ?Sized + Pointable> Default for Atomic<T>

impl<T: ?Sized + Pointable> Default for Shared<'_, T>

impl<T: Default> Default for AtomicCell<T>

impl<T: Default> Default for CachePadded<T>

impl Default for Backoff

impl Default for Parker

impl<T: Default> Default for ShardedLock<T>

impl Default for Builder

impl Default for Target

impl Default for Builder

impl<'a> Default for Env<'a>

impl Default for Chain<'_>

impl<R: Default> Default for DebugAddr<R>

impl<R: Reader, A: UnwindContextStorage<R>> Default for UnwindContext<R, A>

impl<R: Reader> Default for CfaRule<R>

impl Default for Pointer

impl<R: Default> Default for Dwarf<R>

impl<R: Reader> Default for RangeIter<R>

impl<'input, Endian: Default> Default for EndianSlice<'input, Endian> where
    Endian: Endianity

impl<R: Default> Default for DebugAbbrev<R>

impl<R: Default> Default for DebugAranges<R>

impl<R: Default> Default for DebugCuIndex<R>

impl<R: Default> Default for DebugTuIndex<R>

impl<R: Default> Default for DebugLine<R>

impl<R: Default> Default for DebugLoc<R>

impl<R: Default> Default for DebugLocLists<R>

impl<R: Default> Default for LocationLists<R>

impl<R: Default> Default for DebugRanges<R>

impl<R: Default> Default for DebugRngLists<R>

impl<R: Default> Default for RangeLists<R>

impl<R: Default> Default for DebugStr<R>

impl<R: Default> Default for DebugLineStr<R>

impl<R: Default> Default for DebugInfo<R>

impl<R: Default> Default for DebugTypes<R>

impl<'a> Default for CargoOptions<'a>

impl<T, A: Allocator + Clone + Default> Default for RawTable<T, A>

impl<K, V, S, A> Default for HashMap<K, V, S, A> where
    S: Default,
    A: Default + Allocator + Clone

impl<T, S, A> Default for HashSet<T, S, A> where
    S: Default,
    A: Default + Allocator + Clone

impl<K, V, S> Default for IndexMap<K, V, S> where
    S: Default

impl<T, S> Default for IndexSet<T, S> where
    S: Default

impl Default for Buffer

impl Default for RunStats

impl Default for CpuSet

impl Default for Dqblk

impl Default for FdSet

impl<T> Default for AlgSetKey<T>

impl Default for FsFlags

impl<E: Default + Endian> Default for U16Bytes<E>

impl<E: Default + Endian> Default for U32Bytes<E>

impl<E: Default + Endian> Default for U64Bytes<E>

impl<E: Default + Endian> Default for I16Bytes<E>

impl<E: Default + Endian> Default for I32Bytes<E>

impl<E: Default + Endian> Default for I64Bytes<E>

impl<'data> Default for Bytes<'data>

impl<'data, R: ReadRef<'data>> Default for StringTable<'data, R>

impl<'data> Default for SectionTable<'data>

impl<'data, Elf: Default + FileHeader, R: Default> Default for SectionTable<'data, Elf, R> where
    R: ReadRef<'data>,
    Elf::SectionHeader: Default

impl<'data, Elf: FileHeader, R: ReadRef<'data>> Default for SymbolTable<'data, Elf, R>

impl<'data> Default for Version<'data>

impl<'data, Elf: FileHeader> Default for VersionTable<'data, Elf>

impl<'data, E: Default + Endian> Default for LoadCommandIterator<'data, E>

impl<'data, Mach: MachHeader, R: ReadRef<'data>> Default for SymbolTable<'data, Mach, R>

impl<'data> Default for RelocationBlockIterator<'data>

impl<'data> Default for ObjectMap<'data>

impl<'data> Default for ObjectMapEntry<'data>

impl<E: Default + Endian> Default for Sym32<E>

impl<E: Default + Endian> Default for Sym64<E>

impl<T> Default for OnceCell<T>

impl<T: Default> Default for Lazy<T>

impl<T> Default for OnceCell<T>

impl<T: Default> Default for Lazy<T>

impl Default for OnceBool

impl<T> Default for OnceBox<T>

impl Default for &RawOsStr

impl Default for Style

impl Default for Time

impl<B> Default for Control<B>

impl<N, VM> Default for Dfs<N, VM> where
    VM: Default

impl<N, VM> Default for DfsPostOrder<N, VM> where
    VM: Default

impl<N, VM> Default for Bfs<N, VM> where
    VM: Default

impl<N, VM> Default for Topo<N, VM> where
    VM: Default

impl<E: Default, Ix: Default> Default for List<E, Ix> where
    Ix: IndexType

impl<N, VM> Default for DfsSpace<N, VM> where
    VM: VisitMap<N> + Default

impl<N> Default for TarjanScc<N>

impl<N, E, Ty, Ix> Default for Csr<N, E, Ty, Ix> where
    Ty: EdgeType,
    Ix: IndexType

impl<Ix: Default> Default for NodeIndex<Ix>

impl<Ix: Default> Default for EdgeIndex<Ix>

impl<N, E, Ty, Ix> Default for Graph<N, E, Ty, Ix> where
    Ty: EdgeType,
    Ix: IndexType

impl Default for Buffer

impl Default for Map<String, Value>

impl<'a> Default for PrettyFormatter<'a>

impl Default for Value

impl<A: Array> Default for SmallVec<A>

impl Default for Abstract

impl Default for As

impl Default for Async

impl Default for Auto

impl Default for Await

impl Default for Become

impl Default for Box

impl Default for Break

impl Default for Const

impl Default for Continue

impl Default for Crate

impl Default for Default

impl Default for Do

impl Default for Dyn

impl Default for Else

impl Default for Enum

impl Default for Extern

impl Default for Final

impl Default for Fn

impl Default for For

impl Default for If

impl Default for Impl

impl Default for In

impl Default for Let

impl Default for Loop

impl Default for Macro

impl Default for Match

impl Default for Mod

impl Default for Move

impl Default for Mut

impl Default for Override

impl Default for Priv

impl Default for Pub

impl Default for Ref

impl Default for Return

impl Default for SelfType

impl Default for Static

impl Default for Struct

impl Default for Super

impl Default for Trait

impl Default for Try

impl Default for Type

impl Default for Typeof

impl Default for Union

impl Default for Unsafe

impl Default for Unsized

impl Default for Use

impl Default for Virtual

impl Default for Where

impl Default for While

impl Default for Yield

impl Default for Add

impl Default for AddEq

impl Default for And

impl Default for AndAnd

impl Default for AndEq

impl Default for At

impl Default for Bang

impl Default for Caret

impl Default for CaretEq

impl Default for Colon

impl Default for Colon2

impl Default for Comma

impl Default for Div

impl Default for DivEq

impl Default for Dollar

impl Default for Dot

impl Default for Dot2

impl Default for Dot3

impl Default for DotDotEq

impl Default for Eq

impl Default for EqEq

impl Default for Ge

impl Default for Gt

impl Default for Le

impl Default for Lt

impl Default for MulEq

impl Default for Ne

impl Default for Or

impl Default for OrEq

impl Default for OrOr

impl Default for Pound

impl Default for Question

impl Default for RArrow

impl Default for LArrow

impl Default for Rem

impl Default for RemEq

impl Default for FatArrow

impl Default for Semi

impl Default for Shl

impl Default for ShlEq

impl Default for Shr

impl Default for ShrEq

impl Default for Star

impl Default for Sub

impl Default for SubEq

impl Default for Tilde

impl Default for Brace

impl Default for Bracket

impl Default for Paren

impl Default for Group

impl Default for Generics

impl<T, P> Default for Punctuated<T, P>

impl Default for FirstFit

impl Default for Map<String, Value>

impl Default for XxHash64

impl Default for XxHash32

impl Default for Hash64

impl Default for Hash128

impl Default for Parser

impl Default for Params

impl Default for Parser