// ./src/trait/impl_trait.md fn parse_csv_document(src: R) -> std::io::Result>> { src.lines() .map(|line| { // For each line in the source line.map(|line| { // If the line was read successfully, process it, if not, return the error line.split(',') // Split the line separated by commas .map(|entry| String::from(entry.trim())) // Remove leading and trailing whitespace .collect() // Collect all strings in a row into a Vec }) }) .collect() // Collect all lines into a Vec> } /* fn parse_csv_document(src: impl std::io::BufRead) -> std::io::Result>> { src.lines() .map(|line| { // For each line in the source line.map(|line| { // If the line was read successfully, process it, if not, return the error line.split(',') // Split the line separated by commas .map(|entry| String::from(entry.trim())) // Remove leading and trailing whitespace .collect() // Collect all strings in a row into a Vec }) }) .collect() // Collect all lines into a Vec> } */ use std::iter; use std::vec::IntoIter; // This function combines two `Vec` and returns an iterator over it. // Look how complicated its return type is! fn combine_vecs_explicit_return_type( v: Vec, u: Vec, ) -> iter::Cycle, IntoIter>> { v.into_iter().chain(u.into_iter()).cycle() } // This is the exact same function, but its return type uses `impl Trait`. // Look how much simpler it is! fn combine_vecs( v: Vec, u: Vec, ) -> impl Iterator { v.into_iter().chain(u.into_iter()).cycle() } fn part0() { let v1 = vec![1, 2, 3]; let v2 = vec![4, 5]; let mut v3 = combine_vecs(v1, v2); assert_eq!(Some(1), v3.next()); assert_eq!(Some(2), v3.next()); assert_eq!(Some(3), v3.next()); assert_eq!(Some(4), v3.next()); assert_eq!(Some(5), v3.next()); println!("all done"); } // Returns a function that adds `y` to its input fn make_adder_function(y: i32) -> impl Fn(i32) -> i32 { let closure = move |x: i32| { x + y }; closure } fn part1() { let plus_one = make_adder_function(1); assert_eq!(plus_one(2), 3); } fn double_positives<'a>(numbers: &'a Vec) -> impl Iterator + 'a { numbers .iter() .filter(|x| x > &&0) .map(|x| x * 2) } fn part2() { let singles = vec![-3, -2, 2, 3]; let doubles = double_positives(&singles); assert_eq!(doubles.collect::>(), vec![4, 6]); } pub fn main() { part0(); part1(); part2(); }