I am trying to return a vector of closures from a function in Rust. Each of which will ultimately be stored on a struct which holds a Box<dyn Fn(f32) -> f32>
. Here is a simple reproduction of the error:
fn something() -> Vec<Box<dyn Fn(f32) -> f32>> {
let mut vec = Vec::new();
for i in 0..5 {
vec.push(
Box::new(
|t : f32| { t + 1.0 }
)
);
}
vec
}
error[E0308]: mismatched types
--> src\main.rs:13:5
|
2 | fn something() -> Vec<Box<dyn Fn(f32) -> f32>> {
| ---------------------------- expected `Vec<Box<(dyn Fn(f32) -> f32 + 'static)>>` because of return
type
...
8 | |t : f32| { t + 1.0 }
| --------------------- the found closure
...
13 | vec
| ^^^ expected trait object `dyn Fn`, found closure
|
= note: expected struct `Vec<Box<(dyn Fn(f32) -> f32 + 'static)>>`
found struct `Vec<Box<[closure@src\main.rs:8:17: 8:38]>>`
I don't understand this error and where it is coming from, given that the provided closure implements the required trait. I am new to Rust and struggling the most with closures.