首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何从函数返回过滤器迭代器?

如何从函数返回过滤器迭代器?
EN

Stack Overflow用户
提问于 2014-12-25 11:48:19
回答 2查看 6.4K关注 0票数 33

我想要这样的东西:

代码语言:javascript
复制
fn filter_one<'a, T: Int>(input: &'a Vec<T>) -> ??? {
    input.iter().filter(|&x| x == Int::one())
}

函数的返回类型是什么?(我想退货)

(我希望这不是太明显,我已经尝试了半个小时了,现在才开始感到沮丧:p )

编辑:

我试着遵循这里 => 游戏笔链接的指示

编译器给出了以下错误:

代码语言:javascript
复制
<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) -> boolSized?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-12-25 16:49:26

锈1.26

代码语言:javascript
复制
fn filter_one(input: &[u8]) -> impl Iterator<Item = &u8> {
    input.iter().filter(|&&x| x == 1)
}

fn main() {
    let nums = vec![1, 2, 3, 1, 2, 3];
    let other: Vec<_> = filter_one(&nums).collect();
    println!("{:?}", other);
}

锈1.0

代码语言:javascript
复制
fn filter_one<'a>(input: &'a [u8]) -> Box<Iterator<Item = &'a u8> + 'a> {
    Box::new(input.iter().filter(|&&x| x == 1))
}

fn main() {
    let nums = vec![1, 2, 3, 1, 2, 3];
    let other: Vec<_> = filter_one(&nums).collect();
    println!("{:?}", other);
}

此解决方案需要额外的分配。我们创建一个盒式特征对象。在这里,对象的大小总是已知的(它只是一两个指针),但是不需要知道堆中对象的大小。

作为弗拉基米尔·马特维耶夫指出,如果谓词逻辑不需要来自环境的任何信息,则可以使用函数而不是闭包:

代码语言:javascript
复制
use std::{iter::Filter, slice::Iter};

fn filter_one<'a>(input: &'a [u8]) -> Filter<Iter<u8>, fn(&&u8) -> bool> {
    fn is_one(a: &&u8) -> bool {
        **a == 1
    }

    input.iter().filter(is_one)
}

fn main() {
    let nums = vec![1, 2, 3, 1, 2, 3];
    let other: Vec<_> = filter_one(&nums).collect();
    println!("{:?}", other);
}

另请参阅:

票数 29
EN

Stack Overflow用户

发布于 2014-12-25 14:45:23

不幸的是,不可能返回依赖于闭包的迭代器(特别是闭包,函数将正常工作;参见下面),比如filter()map()适配器返回的迭代器。这就是为什么。

这是filter()迭代器扩展方法的签名:

代码语言:javascript
复制
fn filter<P>(self, predicate: P) -> Filter<A, Self, P> where P: FnMut(&A) -> bool

注意,闭包参数接受实现FnMut特性的任何类型。这是正确的,大多数标准库(如果不是全部的话)最近已经被转换为使用未装箱的闭包,而不是旧的盒式闭包。

但是,这个签名意味着,如果将闭包指定为filter()的参数,就像在filter_one()中那样

代码语言:javascript
复制
input.iter().filter(|&x| x == Int::one())

然后,在这个特定调用的单体化之后,P将成为编译器生成的某种匿名的未命名类型。当然,由于它是未命名的,所以不能在类型签名中指定它,因此,您也不能指定依赖于未装箱闭包的迭代器的类型--您只是不知道作为Filter<A, I, P>的第三个参数应该写什么。

您可以通过使用函数而不是闭包来解决这个问题,这对于您的用例来说应该足够了:

代码语言:javascript
复制
use std::slice::Items;
use std::iter::Filter;
use std::num::Int;

fn filter_one<T: Int>(input: &[T]) -> Filter<&T, Items<T>, fn(&&T) -> bool> {
    fn equal_to_one<U: Int>(&&x: &&U) -> bool { x == Int::one() }
    input.iter().filter(equal_to_one::<T>)
}

fn main() {
    let v = [1i, 2, 3];
    let r: Vec<int> = filter_one(v.as_slice()).map(|x| *x).collect();
    println!("{}", r);
}

注意,我还将&Vec<T>更改为&[T] --您不应该使用&Vec<T>,因为它不必要地限制了代码的灵活性。

然而,对于更一般的闭包情况,没有抽象的返回类型是不可能做到的。有人提议增加它们,但推迟到1.0之后。

票数 14
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27646925

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档