Tutorial by Examples

Iterator methods can be broken into two distinct groups: Adapters Adapters take an iterator and return another iterator // Iterator Adapter // | | let my_map = (1..6).map(|x| x * x); println!("{:?}", my_map); Output Map { iter: 1..6 } Note that the v...
fn is_prime(n: u64) -> bool { (2..n).all(|divisor| n % divisor != 0) } Of course this is isn't a fast test. We can stop testing at the square root of n: (2..n) .take_while(|divisor| divisor * divisor <= n) .all(|divisor| n % divisor != 0)
struct Fibonacci(u64, u64); impl Iterator for Fibonacci { type Item = u64; // The method that generates each item fn next(&mut self) -> Option<Self::Item> { let ret = self.0; self.0 = self.1; self.1 += ret; Some(ret) ...

Page 1 of 1