Trait rustdoc::Default 1.0.0[−][src]
pub(crate) 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,
}RunHow 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();
}RunNow, 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() };
}RunDerivable
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 }
}RunExamples
#[derive(Default)]
struct SomeOptions {
foo: i32,
bar: f32,
}RunRequired 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();RunMaking your own:
enum Kind {
A,
B,
C,
}
impl Default for Kind {
fn default() -> Self { Kind::A }
}RunImplementations on Foreign Types
Constructs a new RandomState.
Creates a new DefaultHasher using new.
See its documentation for more.
Creates an UnsafeCell, with the Default value for T.
impl<T, const LANES: usize> Default for Mask<T, LANES> where
T: MaskElement,
LaneCount<LANES>: SupportedLaneCount,
impl<T, const LANES: usize> Default for Mask<T, LANES> where
T: MaskElement,
LaneCount<LANES>: SupportedLaneCount,
Creates an AtomicBool initialized to false.
impl<T, const LANES: usize> Default for Simd<T, LANES> where
T: SimdElement + Default,
LaneCount<LANES>: SupportedLaneCount,
impl<T, const LANES: usize> Default for Simd<T, LANES> where
T: SimdElement + Default,
LaneCount<LANES>: SupportedLaneCount,
Creates an empty LinkedList<T>.
Creates an empty BinaryHeap<T>.
Return an empty ArrayString
impl Default for MatchKind
impl Default for MatchKind
The default match kind is MatchKind::Standard.
pub fn default() -> MatchKind
impl Default for Subscriber<DefaultFields, Format<Full, SystemTime>, LevelFilter, fn() -> Stdout>
impl Default for Subscriber<DefaultFields, Format<Full, SystemTime>, LevelFilter, fn() -> Stdout>
pub fn default(
) -> Subscriber<DefaultFields, Format<Full, SystemTime>, LevelFilter, fn() -> Stdout>
impl Default for SubscriberBuilder<DefaultFields, Format<Full, SystemTime>, LevelFilter, fn() -> Stdout>
impl Default for SubscriberBuilder<DefaultFields, Format<Full, SystemTime>, LevelFilter, fn() -> Stdout>
pub fn default(
) -> SubscriberBuilder<DefaultFields, Format<Full, SystemTime>, LevelFilter, fn() -> Stdout>
impl Default for Style
impl Default for Style
pub fn default() -> Style
pub fn default() -> Style
Returns a style with no properties set. Formatting text using this style returns the exact same text.
use ansi_term::Style;
assert_eq!(None, Style::default().foreground);
assert_eq!(None, Style::default().background);
assert_eq!(false, Style::default().is_bold);
assert_eq!("txt", Style::default().paint("txt").to_string());Runpub fn default() -> ReentrantMutex<R, G, T>
pub fn default() -> RwLock<R, T>
pub fn default() -> Mutex<R, T>
pub fn default() -> Shared<'g, T>
The default value is Value::Null.
This is useful for handling omitted Value fields when deserializing.
Examples
use serde_json::Value;
#[derive(Deserialize)]
struct Settings {
level: i32,
#[serde(default)]
extras: Value,
}
let data = r#" { "level": 42 } "#;
let s: Settings = serde_json::from_str(data)?;
assert_eq!(s.level, 42);
assert_eq!(s.extras, Value::Null);Run