在 Rust 中返回一个更简单的 Iterator 接口而不是 Map [英] Returning a simpler Iterator interface instead of a Map in Rust
问题描述
我想写这个:
fn fibs() -> std::iter::Iterator<int> {
return std::iter::iterate((1i, 1i), |(a, b)| { (b, a + b) }).map(|(a, _)| a)
}
但是如果我这样做,我会收到此错误:
But if I do, I get this error:
error: explicit lifetime bound required
fn fibs() -> std::iter::Iterator<int> {
^^^^^^^^^^^^^^^^^^^^^^^^
如果我写出完整的界面,它会编译:
If, instead I write out the full interface, it compiles:
fn fibs() -> std::iter::Map<'static, (int, int), int, std::iter::Iterate<'static, (int, int)>> {
return std::iter::iterate((1i, 1i), |(a, b)| { (b, a + b) }).map(|(a, _)| a)
}
有没有办法返回更简单的界面?
Is there a way to return the simpler interface?
推荐答案
目前没有任何方法可以返回抽象类型,例如 Iterator
,就像你不能赋值一样作为变量的类型(let x: Iterator
不会因为同样的原因编译).
There is not at present any way to return an abstract type such as Iterator<T>
, just as you can’t assign that as the type of a variable (let x: Iterator<uint>;
won’t compile for the same reasons).
这个已经讨论过,当然是需要的;完成后,它可能会采用 fn fibs() -> 的形式.impl Iterator
,但你还不能这样做.
This has been discussed and is certainly desired; when it is done it will probably be in the form fn fibs() -> impl Iterator<uint>
, but you can’t do that yet.
这篇关于在 Rust 中返回一个更简单的 Iterator 接口而不是 Map的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!