Default for composite struct
examples/struct/default-for-composite-struct/src/main.rs
#![allow(clippy::derivable_impls)] #[derive(Debug)] #[allow(dead_code)] struct Something { name: String, input: Input, } #[derive(Debug)] #[allow(dead_code)] struct Input { name: String, } impl Something { pub fn new() -> Something { Something { name: String::new(), input: Input::new(), } } } impl Input { pub fn new() -> Input { Input { name: String::new(), } } } impl Default for Something { fn default() -> Something { Something { name: String::new(), input: Input { ..Input::default() }, } } } impl Default for Input { fn default() -> Input { Input { name: String::new(), } } } fn main() { let sg = Something { name: String::from("Foo Bar"), input: Input { name: String::from("input text"), }, }; println!("{:?}", sg); let new = Something::new(); println!("{:?}", new); let empty = Something { ..Something::default() }; println!("{:?}", empty); let with_name = Something { name: String::from("Hello"), ..Something::default() }; println!("{:?}", with_name); }
[src/main.rs:58:5] sg = Something { name: "Foo Bar", input: Input { name: "input text", }, } [src/main.rs:61:5] new = Something { name: "", input: Input { name: "", }, } [src/main.rs:66:5] empty = Something { name: "", input: Input { name: "", }, } [src/main.rs:73:5] with_name = Something { name: "Hello", input: Input { name: "", }, }