Default

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);
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.168.8