as @ChrisMorgan said, until you can return an abstract type. If you are creating a public API and want to avoid being overly dependent on a particular iterate and map implementation, you can encapsulate the return type in your own structure (which is more or less what the collections in the std library themselves do).
Sort of:
// updated to rustc 0.13.0-nightly (2015-01-02) use std::iter::{Map, iterate, Unfold}; type Fibs = Map<(int, int), int, Unfold<(int, int), (fn((int, int)) -> (int, int), Option<(int, int)>, bool), fn(&mut (fn((int, int)) -> (int, int), Option<(int, int)>, bool)) -> Option<(int, int)>>, fn((int, int)) -> int>; struct Fibs2 { iter: Fibs, } impl Fibs2 { fn iter() -> Fibs2 { fn inner((a, b): (int, int)) -> (int, int) { (b, a + b) } let in_fn = inner as fn((int, int)) -> (int, int); fn first((a, _): (int, int)) -> int { a } let p_first = first as fn((int, int)) -> int; Fibs2{ iter: iterate((1i, 1i), in_fn).map(p_first) } } } impl Iterator<int> for Fibs2 { fn next(&mut self) -> Option<int> { self.iter.next() } } fn main() { for fib_n in Fibs2::iter().take(10) { println!("{}", fib_n); } }
source share