October 2018
Beginner
180 pages
4h 48m
English
When the Default trait is implemented for a type, it makes it possible for us to request a default value for that type of data.
When we derive Default for a data type, it sets the default value for that type to be made up of the default values for all of the contained data types, so when we do this:
#[derive(Default)]pub struct DefaultExample { name: String, value: i32,}
What we're doing is setting the default for the DefaultExample type to be a DefaultExample containing the default values of a String and an i32.
We can request a default value like this:
let x: DefaultExample = Default::default();println!("Default String is {:?}, default i32 is {:?}", x.name, x.value);
Read now
Unlock full access