Trait std::default::Default 1.0.0[−][src]
pub trait Default { fn default() -> Self; }
Expand description
trait,用于为类型提供有用的默认值。
有时,您希望回退到某种默认值,而不必特别关心它的含义。
这经常出现在定义一组选项的 struct
中:
struct SomeOptions { foo: i32, bar: f32, }Run
我们如何定义一些默认值? 您可以使用 Default
:
#[derive(Default)] struct SomeOptions { foo: i32, bar: f32, } fn main() { let options: SomeOptions = Default::default(); }Run
现在,您将获得所有默认值。Rust 为各种基本类型实现 Default
。
如果要覆盖特定选项,但仍保留其他默认值:
fn main() { let options = SomeOptions { foo: 42, ..Default::default() }; }Run
Derivable
如果类型的所有字段都实现 Default
,则此 trait 可以与 #[derive]
一起使用。
当 derive
d 时,它将为每个字段的类型使用默认值。
如何实现 Default
?
提供 default()
方法的实现,该实现返回您类型的值,该值应为默认值:
enum Kind { A, B, C, } impl Default for Kind { fn default() -> Self { Kind::A } }Run
Examples
#[derive(Default)] struct SomeOptions { foo: i32, bar: f32, }Run
Required methods
返回类型的 “default value”。
默认值通常是某种初始值,标识值或其他可能有意义的默认值。
Examples
使用内置的默认值:
let i: i8 = Default::default(); let (x, y): (Option<String>, f64) = Default::default(); let (a, b, (c, d)): (i32, u32, (bool, bool)) = Default::default();Run
自己制作:
enum Kind { A, B, C, } impl Default for Kind { fn default() -> Self { Kind::A } }Run
Implementors
pub fn default() -> Box<str, Global>ⓘNotable traits for Box<I, A>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<R: Read + ?Sized> Read for Box<R>impl<W: Write + ?Sized> Write for Box<W>
[src]Notable traits for Box<I, A>
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<R: Read + ?Sized> Read for Box<R>impl<W: Write + ?Sized> Write for Box<W>
fn default() -> Box<CStr>ⓘNotable traits for Box<I, A>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<R: Read + ?Sized> Read for Box<R>impl<W: Write + ?Sized> Write for Box<W>
[src]Notable traits for Box<I, A>
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<R: Read + ?Sized> Read for Box<R>impl<W: Write + ?Sized> Write for Box<W>
fn default() -> Box<OsStr>ⓘNotable traits for Box<I, A>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<R: Read + ?Sized> Read for Box<R>impl<W: Write + ?Sized> Write for Box<W>
[src]Notable traits for Box<I, A>
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<R: Read + ?Sized> Read for Box<R>impl<W: Write + ?Sized> Write for Box<W>
使用 new
创建一个新的 DefaultHasher
。
有关更多信息,请参见其文档。
创建一个新的 RandomState
。
创建一个初始化为 false
的 AtomicBool
。
pub fn default() -> Box<[T], Global>ⓘNotable traits for Box<I, A>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<R: Read + ?Sized> Read for Box<R>impl<W: Write + ?Sized> Write for Box<W>
[src]Notable traits for Box<I, A>
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<R: Read + ?Sized> Read for Box<R>impl<W: Write + ?Sized> Write for Box<W>
pub fn default() -> Box<T, Global>ⓘNotable traits for Box<I, A>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<R: Read + ?Sized> Read for Box<R>impl<W: Write + ?Sized> Write for Box<W>
[src]
pub fn default() -> Box<T, Global>ⓘNotable traits for Box<I, A>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<R: Read + ?Sized> Read for Box<R>impl<W: Write + ?Sized> Write for Box<W>
[src]Notable traits for Box<I, A>
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<R: Read + ?Sized> Read for Box<R>impl<W: Write + ?Sized> Write for Box<W>
创建一个 Box<T>
,其 T 值为 Default
。
创建一个 UnsafeCell
,其 T 值为 Default
。
创建一个空的 BinaryHeap<T>
。
创建一个空的 LinkedList<T>
。