- derivable_impls
Empty string and zero as default values
- In this special case the default values we set in the implementation of the Default trait happen to be the default values of each primitive type. (empty string for strings and 0 for numbers).
- In this case clippy will indicate that we don't need to implement Default by ourselves.
- In this example we silences clippy, in the next example we derive from Default.
examples/struct/default/src/main.rs
#![allow(clippy::derivable_impls)] #[derive(Debug)] #[allow(dead_code)] struct Something { name: String, number: i32, } impl Default for Something { fn default() -> Something { Something { name: String::new(), number: 0, } } } fn main() { let sg = Something { name: String::from("Foo Bar"), number: 42, //..Default::default() }; println!("{:?}", sg); assert_eq!(sg.name, "Foo Bar"); assert_eq!(sg.number, 42); let empty = Something { ..Default::default() }; println!("{:?}", empty); assert_eq!(empty.name, ""); assert_eq!(empty.number, 0); let with_name = Something { name: String::from("Hello"), ..Default::default() }; println!("{:?}", with_name); assert_eq!(with_name.name, "Hello"); assert_eq!(with_name.number, 0); let with_number = Something { number: 42, ..Default::default() }; println!("{:?}", with_number); assert_eq!(with_number.name, ""); assert_eq!(with_number.number, 42); }