'Is there a way to convert to a type from multiple other types in Rust?

I'm in a situation where I'm frequently composing a struct from two other types:

struct Foo {
    a: usize,
    b: usize,
}

enum Bar {
    X,
    Y,
    Z,
}

struct Baz {
    bar: Bar,
    a: usize,
    b: usize,
}

impl Baz {
    fn new(bar: Bar, a: usize, b: usize) -> Self {
        Baz { bar, a, b }
    }
}

fn main() {
    let foo = Foo { a: 1, b: 2 };
    let bar = Bar::X;

    let baz = Baz::new(bar, foo.a, foo.b);
}

I would love to use something like the From trait here, but given that I'm pulling from two different types, I don't think it's possible. Is there a cleaner or more idiomatic way to accomplish this or should I just use a macro?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source