[][src]Macro assign::assign

macro_rules! assign {
    ($initial_value:expr, {
        $($field:ident: $value:expr),+ $(,)?
    }) => { ... };
}

Mutate instance with declarative flavor

Usage

#[non_exhaustive]
struct SomeStruct {
    a: u32,
    b: Option<f32>,
    c: String,
}
impl SomeStruct {
    fn new() -> SomeStruct {
        SomeStruct {
            a: 1u32,
            b: None,
            c: String::from("old"),
        }
    }
}

// In order to treat the mutation of field `a` and `c` as an initialization
// Use assign to mutate field in declarative flavor, thus avoiding the risk inserting code
// between the line that defines a field and the line that defines the other
// Note that field `b` is skipped
let instance = assign!(SomeStruct::new(), {
    a: 2u32,
    c: String::from("new"),
});

// Equivalent
let instance2 = {
    let mut item = SomeStruct::new();
    item.a = 2u32;
    item.c = String::from("new");
    item
};

assert_eq!(instance.a, instance2.a);
assert_eq!(instance.b, instance2.b);
assert_eq!(&instance.c, &instance2.c);