简体   繁体   English

如何将明确的生命周期应用于返回的特征?

[英]How do I apply an explicit lifetime bound to a returned trait?

Returning an iterator from a function in Rust is an exercise of Sisyphean dimensions, but I am told it's possible to return one as a trait without quite so much pain. 从Rust中的函数返回迭代器是Sisyphean维度的一种练习,但是我被告知有可能在没有太多麻烦的情况下将一个迭代器作为特征返回。 Unfortunately, it isn't working: apparently, I need an explicit lifetime bound? 不幸的是,它不起作用:显然,我需要明确的终身限制吗? Which is apparently not the same thing as adding a lifetime parameter. 这显然与添加生命周期参数不同。 Which means I have no idea how to do that. 这意味着我不知道该怎么做。

Here's my (tiny, test) code: 这是我的(小测试)代码:

fn main() {
    let args = get_args();

    for arg in args {
        println!("{}", arg);
    }
}

fn get_args() -> Iterator {
    std::env::args().filter_map(|arg| arg.into_string().ok())
}

What is the appropriate way to make this actually work ? 有什么合适的方法可以使其真正起作用

Edit: rust version rustc 1.0.0-nightly (00df3251f 2015-02-08 23:24:33 +0000) 编辑:锈版本rustc 1.0.0-每晚(00df3251f 2015-02-08 23:24:33 +0000)

You can't return a bare Iterator from a function, because it is a trait, thus not a sized type. 您不能从函数返回裸Iterator ,因为它是特征,因此不是大小类型。

In your situation, you'll need to put the iterator object inside a box, in order to make it into a sized object that can be returned from the function. 在您的情况下,您需要将迭代器对象放在一个框内,以使其成为可以从函数返回的大小合适的对象。

To do so, you can change your code like this: 为此,您可以这样更改代码:

fn get_args() -> Box<Iterator<Item=String> + 'static> {
    Box::new(std::env::args().filter_map(|arg| arg.into_string().ok()))
}

Here I've added a lifetime specifier 'static for the trait object, meaning that it is completely self-owned (a function taking no arguments will almost always return something valid for the 'static lifetime in this sense). 在这里,我为trait对象添加了一个生存期说明符'static ”,这意味着它是完全自拥有的(在这种意义上,不带参数的函数几乎总是会返回对'static life 'static有效的值)。

You also need the <Item=String> part to explicit the type of data yielded by your iterator. 您还需要<Item=String>部分来明确说明迭代器产生的数据类型。 In this case: Strings. 在这种情况下:字符串。

In this specific case you can manage to return a concrete type from your get_args, like so: 在这种特定情况下,您可以设法从get_args返回具体类型,如下所示:

fn get_args() -> FilterMap<Args, fn(OsString) -> Option<String>> {
    fn arg_into_string(arg: OsString) -> Option<String> { arg.into_string().ok() }
    args().filter_map(arg_into_string as fn(OsString) -> Option<String>)
}

basically this applies to all the cases where the closure you use in the iterator adapter (in your case filter_map) is not really a closure, in that it does not capture any environment, and it can be modeled by a plain old function. 基本上,这适用于所有在迭代器适配器中使用的闭包(在您的情况下为filter_map)不是真正的闭包的情况,因为它无法捕获任何环境,并且可以通过普通的旧函数进行建模。

In general, if you do need to return a type that does contain a closure, you will indeed need to box it and return a trait object. 通常,如果确实需要返回包含闭包的类型,则确实需要将其装箱并返回特征对象。 In your case: 在您的情况下:

fn get_args() -> Box<Iterator<Item=String> + 'static> {
    Box::new(std::env::args().filter_map(|arg| arg.into_string().ok()))
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Rust:如何限制 Iterator::next() 的生命周期? - Rust: how to bound the lifetime of Iterator::next()? 实现迭代器特征时如何修复生命周期不匹配? - How to fix lifetime mismatch when implementing Iterator Trait? 为什么这个迭代器需要一个生命周期,我该如何给它一个生命周期? - Why does this iterator require a lifetime, and how do I give it one? 如何使用Iterator特征构建通用API - How do I use the Iterator trait to build generic APIs 如何为我的结构实现 FromIterator 特征? - How do I implement a FromIterator trait for my struct? 如何将迭代器适配器与返回 impl Trait 作为 IntoIter 关联类型的 IntoIterator 的函数一起使用? - How do I use a iterator adapter with a function returning impl Trait as the IntoIter associated type of IntoIterator? 如何将具体类型的迭代器特征对象转换为特征对象的迭代器特征对象? - How can I transform an iterator trait object of concrete types into an iterator trait object of trait objects? 如何为特征实现“默认迭代器”? - How can I implement a “default iterator” for a trait? 从函数返回的迭代器的生存期要求冲突 - Conflicting lifetime requirement for iterator returned from function 类型不匹配,将迭代器项解析为具有显式寿命的指针 - Type mismatch resolving iterator Item to a pointer with an explicit lifetime
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM