我想将返回迭代器的 lambda 存储在结构中。需要 lambda 是因为并非所有容器都实现了iter()
函数(例如String::chars()
),所以我需要一种通用的方法来从容器中获取迭代器。
use std::marker::PhantomData;
struct Foo<F, V, T> {
foo: F,
ph: PhantomData<V>,
ph2: PhantomData<T>,
}
impl<F, V, T> Foo<F, V, T> where
F: Fn(V) -> dyn Iterator<Item = T> {
}
不幸的是,我收到以下错误:
error[E0277]: the size for values of type `(dyn std::iter::Iterator<Item=T> + 'static)` cannot be known at compilation time
--> main.rs:9:1
|
9 | / impl<F, V, T> Foo<F, V, T> where
10 | | F: Fn(V) -> dyn Iterator<Item = T>
11 | | {
12 | |
13 | | }
| |_^ doesn't have a size known at compile-time
|
= help: the trait `std::marker::Sized` is not implemented for `(dyn std::iter::Iterator<Item=T> + 'static)`
= note: to learn more, visit <https://doc.rust-lang.org/book/second-edition/ch19-04-advanced-types.html#dynamically-sized-types-and-the-sized-trait>
= note: required by `std::ops::FnOnce`
我希望我理解它的含义,但我不知道如何解决它。