el_diablo/src/level_ladder.rs

31 lines
680 B
Rust
Raw Normal View History

2024-11-06 14:29:30 +01:00
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;
2024-11-07 09:22:19 +01:00
self.next += current;
2024-11-06 14:29:30 +01:00
2024-11-07 09:22:19 +01:00
// Since there's no endpoint to a Fibonacci sequence, the `Iterator`
2024-11-06 14:29:30 +01:00
// 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));
}