-1
我是Rust的初学者,我想实现一个以u8
或&u8
的迭代器作为输入并输出一些块的适配器。Rust自定义适配器和泛型
我不知道如何让我的程序编译。我在不使用ends_with_separator()
的情况下使适配器工作,其中迭代器的类型没有限制,但现在我需要定义我的适配器,以便它只接受u8
或&u8
的迭代器,我不知道该怎么做。
pub struct Chunk<T> {
pub data: Vec<T>,
}
pub struct Chunker<I> {
pub iter: I,
}
impl<I> Chunker<I> {
fn ends_with_separator(buf: &Vec<u8>) -> bool {
match buf.last() {
Some(v) => v % 7 == 0,
None => true,
}
}
}
impl<I: Iterator> Iterator for Chunker<I> {
type Item = Chunk<I::Item>;
#[inline]
fn next(&mut self) -> Option<Self::Item> {
let mut buf = Vec::new();
while let Some(v) = self.iter.next() {
buf.push(v);
if Self::ends_with_separator(&buf) {
return Some(Chunk { data: buf });
}
}
None
}
}
fn main() {
let chunker = Chunker { iter: (0u8..10) };
for chunk in chunker {
println!("{:?}", chunk.data);
}
}
下一次你有问题:1)降低你的代码展示了同样错误的最小示例,2)*给出错误*。 – mcarton
这是该程序的小版本。我的问题更像是“如何做某事”使用正确的语法。 我没有显示错误信息,因为它在这里没有意义。 – Vincent