【发布时间】:2016-06-14 10:24:29
【问题描述】:
我有一个结构:
enum Bar {
Bar0,
Bar1,
Bar2
}
struct Foo {
bar: Bar
}
我有一个函数需要一片 Foos:
fn do_the_thing(foos: &[Foo]) { ... }
需要做的部分事情是在切片的所有“bar”值相同的元素上调用另一个函数:
fn another_func(foos: &[Foo]) { ... }
我尝试过这样的事情:
fn do_the_thing(foos: &[Foo]) {
let bar0_foos = foos.iter().filter(|f| f.bar == Bar0 );
another_func(&bar0_foos);
}
不幸的是,有人告诉我 bar0_foos 是&std::iter::Filter<std::slice::Iter<...>>。我也试过:
fn do_the_thing(foos: &[Foo]) {
let bar0_foos = foos.iter().filter(|f| f.bar == Bar0 ).collect();
another_func(&bar0_foos);
}
在这里,我被告知std::marker::Sized 不满意。
那么,这样做的正确方法是什么?我应该吃片以外的东西吗?
【问题讨论】:
标签: rust