A
is a structure which contains a vector of B
. A
implements the add_b
method which adds a B
instance to the list of B
. B
contains a closure property f
.
If I add one B
to the vector with add_b
, it's OK. If I add two vectors with add_b
, I got an error saying the two closures are different. Here is a minimal example:
// A struct...
struct A<F> {
b_vec: Vec<B<F>>, // A vector of B
}
// ...and its implementation
impl<F> A<F>
where
F: Fn(),
{
fn new() -> A<F> {
A { b_vec: Vec::new() }
}
fn add_b(&mut self, b: B<F>) {
self.b_vec.push(b);
}
}
// B struct...
struct B<F> {
f: F,
}
// ...and its implementation
impl<F> B<F>
where
F: Fn(),
{
fn new(f: F) -> B<F> {
B { f: f }
}
}
// I add two B (with their closures arguments) in A
fn main() {
let mut a = A::new();
a.add_b(B::new(|| println!("test")));
a.add_b(B::new(|| println!("test2")));
}
This code results in:
error[E0308]: mismatched types
--> src/main.rs:39:20
|
39 | a.add_b(B::new(|| println!("test2")));
| ^^^^^^^^^^^^^^^^^^^^ expected closure, found a different closure
|
How can I add multiple B
with their different closures to A
's b_vec
?