I have an enumerate goes like this:
pub enum Foo {
Foo1(Foo1),
Foo2(Foo2),
Foo3(Foo3),
}
in which
pub type Foo1 = Bar<I1>;
pub type Foo2 = Bar<I2>;
in which
struct Bar<I>{
var: I,
...
}
impl Bar{
fn do(word: &str){
...
}
}
here I am writing another function which needs a match
block:
match something {
Foo::Foo1(inner) => {
inner.do(a_word)
}
Foo::Foo2(inner) => {
inner.do(a_word)
}
_ => {
(...)
}
}
inner
is of Bar
struct, but different in Foo1
and Foo2
:
inner
for Foo1
: Bar<I1>
inner
for Foo2
: Bar<I2>
The problem is, in the above block, the operation when something
matches Foo1
and Foo2
is exact the same, but it is not very elegant to repeat the same operations twice.
I tried:
match something {
Foo::Foo1(inner) | Foo::Foo2(inner)=> {
inner.do(a_word)
_ => {
(...)
}
}
It doesn't work, because the inner
in Foo1
and Foo2
are of different types(as mentioned before)
So how can I match Foo1
and Foo2
compactly?
Thank you!