Is there syntax for moving fields between similar structs?

后端 未结 1 1207
轻奢々
轻奢々 2020-12-11 19:40

I have a big struct Foo, and want to map it into a Foo where most of the fields don\'t need updating. I was hoping t

相关标签:
1条回答
  • 2020-12-11 20:07

    Is there syntax for moving fields between similar structs?

    No. There is no such syntax. The current implementation of "struct update" (previously called "functional record update") syntax only allows the exact same type.

    Is there better syntax for this somewhere, or a better way to implement these map-like methods for non-trivial structs?

    No. The only suggestion I have is to destructure your original struct and then recreate it. You also don't need the ::<R> as it's inferred.

    let Foo { a, b, c, d, e, t } = q;
    let r = Foo {
        a,
        b,
        c,
        d,
        e,
        t: fixup(t),
    };
    

    See also:

    • RFC 2528 — Type-changing struct update syntax
    • Issue #47741 — Struct initializer ..x syntax should work for other structs with structurally equal subset of fields
    • Similar issue: Struct update syntax for different types
    0 讨论(0)
提交回复
热议问题