rust-unofficial / patterns

A catalogue of Rust design patterns, anti-patterns and idioms
https://rust-unofficial.github.io/patterns/
Mozilla Public License 2.0
7.85k stars 354 forks source link

Improve mem-replace examples #253

Closed wfxr closed 3 years ago

wfxr commented 3 years ago

Before:

fn a_to_b(e: &mut MyEnum) {
    *e = if let MyEnum::A { ref mut name, x: 0 } = *e {
        MyEnum::B { name: mem::take(name) }
    } else { return }
}

After:

fn a_to_b(e: &mut MyEnum) {
    if let MyEnum::A { name, x: 0 } = e {
        *e = MyEnum::B { name: mem::take(name) }
    }
}
simonsan commented 3 years ago

Thanks for the contribution! :)