通过例子学习Rust

36 高阶函数

Rust provides Higher Order Functions (HOF), these are functions that take a closure as argument to produce a more useful function. HOFs and lazy iterators give Rust its functional flavor.

// The `AdditiveIterator` trait adds the `sum` method to iterators use std::iter::AdditiveIterator; use std::iter; fn main() { println!("Find the sum of all the squared odd numbers under 1000"); let upper = 1000u; // Imperative approach // Declare accumulator variable let mut acc = 0; // Iterate: 0, 1, 2, ... to infinity for n in iter::count(0u, 1) { // Square the number let n_squared = n * n; if n_squared >= upper { // Break loop if exceeded the upper limit break; } else if is_odd(n_squared) { // Accumulate value, if it's odd acc += n_squared; } } println!("imperative style: {}", acc); // Functional approach let sum_of_squared_odd_numbers = // All natural numbers iter::count(0u, 1). // Squared map(|n| n * n). // Below upper limit take_while(|&n| n < upper). // That are odd filter(|n| is_odd(*n)). // Sum them sum(); println!("functional style: {}", sum_of_squared_odd_numbers); } fn is_odd(n: uint) -> bool { n % 2 == 1 }

Option and Iterator implement their fair share of HOFs.