default_macro 0.2.1

My default!() macro
Documentation
  • Coverage
  • 0%
    0 out of 2 items documented0 out of 1 items with examples
  • Size
  • Source code size: 4.58 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 129.40 kB This is the summed size of all files generated by rustdoc for all configured targets
  • Links
  • Homepage
  • mikhail-m1/default_macro
    1 1 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • mikhail-m1

default! macro for Rust

Sometimes you want to create nested structures, set values for some the fields and use default value for most of the other, for example:

use default_macro::default;

#[derive(Default)]
struct Window { title: &str, border: Border, /* 10 other fields */ }

#[derive(Default)]
struct Border { width: f64, /* 5 other fields*/ }

fn foo() {
    let w1 = Window { 
        title: "Test", 
        border: Border {  
            width: 10.0, 
            ..Border::Default}, 
        ..Window::default()
    };

    // with the macros:
    let w2 = default!( Window { 
        title: "Test", 
        border: Border {  width: 10.0} 
    });
}