31 lines
692 B
Rust
31 lines
692 B
Rust
|
pub struct LevelLadder {
|
||
|
curr: usize,
|
||
|
next: usize,
|
||
|
}
|
||
|
|
||
|
impl Iterator for LevelLadder {
|
||
|
type Item = usize;
|
||
|
|
||
|
fn next(&mut self) -> Option<Self::Item> {
|
||
|
let current = self.curr;
|
||
|
|
||
|
self.curr = self.next;
|
||
|
self.next = current + self.next;
|
||
|
|
||
|
// Since there's no endpoint to a Fibonacci sequence, the `Iterator`
|
||
|
// will never return `None`, and `Some` is always returned.
|
||
|
Some(current * 30)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// Returns a Fibonacci sequence generator
|
||
|
pub fn get_level_ladder() -> LevelLadder {
|
||
|
LevelLadder { curr: 1, next: 2 }
|
||
|
}
|
||
|
|
||
|
#[test]
|
||
|
fn test_level_ladder() {
|
||
|
let mut iter = get_level_ladder();
|
||
|
assert_eq!(iter.next(), Some(30));
|
||
|
}
|