I have a big struct Foo<Q>
, and want to map
it into a Foo<R>
where most of the fields don't need updating. I was hoping to use the ..
operator for this, but it is a type error, as they are technically different types.
That is, given:
struct Foo<T> {
a: usize,
b: usize,
t: T,
}
let q: Foo<Q>;
I would like to write:
let r = Foo::<R> {
t: fixup(q.t),
..q
};
But, this gives me a type error:
error[E0308]: mismatched types
|
3 | ..q
| ^ expected struct `R`, found struct `Q`
|
= note: expected type `Foo<R>`
found type `Foo<Q>`
The type error is reasonable, as the types can be thought of as templates in this case.
The only workaround I have is to write out the transformation in full, which gets ugly quite quickly:
let r = Foo::<R> {
a: q.a,
b: q.b,
t: fixup(q.t),
};
Here's a playground with a full test-case, including the compile error and the long-form.
Is there better syntax for this somewhere, or a better way to implement these map
-like methods for non-trivial structs?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…