Struct update syntax makes it easy to create a new struct reusing the data contained in an existing struct of the same type. Is there similar functionality for structs of different types, perhaps using macros?
I'd like to be able to do something like this:
#[derive(Debug)]
struct Foo {
bar: i32,
baz: i32,
}
#[derive(Debug)]
struct Qux {
bar: i32,
}
fn main() {
let spread = Qux { bar: 0 };
let foo = Foo { baz: 2, ..spread };
println!("{:?}", foo);
}
But obviously, this fails:
error[E0308]: mismatched types
--> src/main.rs:15:31
|
15 | let foo = Foo { baz: 2, ..spread };
| ^^^^^^ expected struct `Foo`, found struct `Qux`
|
= note: expected type `Foo`
found type `Qux`
My use case is that I have input coming from different sources which gets parsed into different structs, and ultimately I want to merge these into a single struct which represents a database row and commit it to the database.