Returning a simpler Iterator interface instead of a Map in Rust

前端 未结 2 1272
孤城傲影
孤城傲影 2021-01-12 22:22

I\'d like to write this:

fn fibs() -> std::iter::Iterator {
    return std::iter::iterate((1i, 1i), |(a, b)| { (b, a + b) }).map(|(a, _)| a)
}
         


        
2条回答
  •  南笙
    南笙 (楼主)
    2021-01-12 22:46

    There is not at present any way to return an abstract type such as Iterator, just as you can’t assign that as the type of a variable (let x: Iterator; won’t compile for the same reasons).

    This has been discussed and is certainly desired; when it is done it will probably be in the form fn fibs() -> impl Iterator, but you can’t do that yet.

提交回复
热议问题