【发布时间】:2015-02-23 03:55:25
【问题描述】:
我想要这样的东西:
fn filter_one<'a, T: Int>(input: &'a Vec<T>) -> ??? {
input.iter().filter(|&x| x == Int::one())
}
该函数的返回类型是什么? (我想返回迭代器)
(我希望这不是太明显,我已经尝试了半个小时,现在开始感到沮丧:p)
编辑:
我尝试按照here => playpen link 的指示进行操作
编译器给了我以下错误:
<anon>:5:1: 7:2 error: the trait `core::kinds::Sized` is not implemented for the type `for<'r> core::ops::Fn(&'r T) -> bool + 'a`
<anon>:5 fn filter_one<'a, T: Int>(input: &'a Vec<T>) -> Filter<&T, Iter<'a, T>, Fn(&T) -> bool>{
<anon>:6 input.iter().filter(|&x| x == Int::one())
<anon>:7 }
<anon>:5:1: 7:2 note: required by `core::iter::Filter`
<anon>:5 fn filter_one<'a, T: Int>(input: &'a Vec<T>) -> Filter<&T, Iter<'a, T>, Fn(&T) -> bool>{
<anon>:6 input.iter().filter(|&x| x == Int::one())
<anon>:7 }
<anon>:5:1: 7:2 error: the trait `for<'r> core::ops::Fn(&'r &'a T) -> bool` is not implemented for the type `for<'r> core::ops::Fn(&'r T) -> bool + 'a`
<anon>:5 fn filter_one<'a, T: Int>(input: &'a Vec<T>) -> Filter<&T, Iter<'a, T>, Fn(&T) -> bool>{
<anon>:6 input.iter().filter(|&x| x == Int::one())
<anon>:7 }
<anon>:5:1: 7:2 note: required by `core::iter::Filter`
<anon>:5 fn filter_one<'a, T: Int>(input: &'a Vec<T>) -> Filter<&T, Iter<'a, T>, Fn(&T) -> bool>{
<anon>:6 input.iter().filter(|&x| x == Int::one())
<anon>:7 }
error: aborting due to 2 previous errors
playpen: application terminated with error code 101
我如何告诉rustc Fn(&T) -> bool 是Sized??
【问题讨论】:
-
可能是的,但是编译器没有给我足够的指导,我会编辑!
-
在这种情况下,它可能与How to return a generic Map struct?重复