├── Cargo.toml ├── Cargo.lock └── src └── main.rs /Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "sandbox-iterators-in-rust" 3 | version = "0.1.0" 4 | edition = "2021" 5 | 6 | [dependencies] 7 | -------------------------------------------------------------------------------- /Cargo.lock: -------------------------------------------------------------------------------- 1 | # This file is automatically @generated by Cargo. 2 | # It is not intended for manual editing. 3 | version = 3 4 | 5 | [[package]] 6 | name = "sandbox-iterators-in-rust" 7 | version = "0.1.0" 8 | -------------------------------------------------------------------------------- /src/main.rs: -------------------------------------------------------------------------------- 1 | fn main() { 2 | using_iter_on_vec(); 3 | using_map_and_collect(); 4 | using_closure_with_borrow(); 5 | using_closure_with_mut_borrow(); 6 | using_closure_with_ownership(); 7 | using_fn_with_closure_input(|str| println!("{}", str)); 8 | using_filter(); 9 | using_map_and_filter(); 10 | using_sum_to_consume(); 11 | using_fold_to_accumulate(); 12 | using_into_iter_to_take_ownership(); 13 | using_iter_mut_for_mutation(); 14 | using_enumerate_to_index(); 15 | using_custom_iterator(); 16 | } 17 | 18 | // iterating through a vector 19 | fn using_iter_on_vec() { 20 | let some_vec = vec!["a", "b", "c"]; 21 | let my_iterator = some_vec.iter(); 22 | for val in my_iterator { 23 | println!("Value: {}", val); 24 | } 25 | } 26 | 27 | // mapping and collecting iterms 28 | fn using_map_and_collect() { 29 | let v = vec![0, 1, 2]; 30 | let incremented: Vec<_> = v.iter().map(|x| x + 1).collect(); 31 | println!("{:?}", incremented); 32 | } 33 | 34 | // making a simple closure which captures it's environment by borrowing 35 | fn using_closure_with_borrow() { 36 | let x = 10; 37 | let my_closure = || println!("x is {}", x); 38 | my_closure(); 39 | } 40 | 41 | // making a closure which mutably borrows a value from it's environment 42 | fn using_closure_with_mut_borrow() { 43 | let mut x = 10; 44 | let mut increment_x = |y| x += y; 45 | increment_x(1); 46 | println!("x is {}", x); 47 | } 48 | 49 | // making a closure take ownership of it's environment 50 | fn using_closure_with_ownership() { 51 | let x = String::from("I am owned!"); 52 | let take_x = move || println!("{}", x); 53 | take_x(); 54 | // println!("{}", x); // Error: `x` has been moved 55 | } 56 | 57 | // making a function which uses a closure as input 58 | fn using_fn_with_closure_input(func: F) where F: Fn(String) { 59 | func(String::from("I am injected into the closure!")); 60 | } 61 | 62 | // filtering elements out using filter 63 | fn using_filter() { 64 | let nums = vec![1,2,3,4,5,6,7,8,9,10]; 65 | let even: Vec<_> = nums.iter().filter(|&&x| x % 2 == 0).collect(); 66 | println!("{:?}", even); 67 | } 68 | 69 | // combining methods like map and filter 70 | fn using_map_and_filter() { 71 | let nums = vec![1,2,3,4,5,6]; 72 | let doubled_even: Vec<_> = nums 73 | .iter() 74 | .filter(|&x| x % 2 == 0) 75 | .map(|&x| x * 2) 76 | .collect(); 77 | println!("{:?}", doubled_even); 78 | } 79 | 80 | // consuming an iterator using sum 81 | fn using_sum_to_consume() { 82 | let nums = vec![1,2,3,4]; 83 | let total: i32 = nums.iter().sum(); 84 | println!("{:?}", total); 85 | } 86 | 87 | // chaining operations with fold and building accumulitive values 88 | fn using_fold_to_accumulate() { 89 | let nums = vec![1,2,3,4,5]; 90 | let sum = nums.iter().fold(0, |acc, &x| acc + x); 91 | println!("{:?}", sum); 92 | } 93 | 94 | // using into_iter to consume and take ownership of a collection 95 | fn using_into_iter_to_take_ownership() { 96 | let nums = vec![1,2,3,4,5]; 97 | for num in nums.into_iter() { 98 | println!("{}", num); 99 | } 100 | // nums is no longer valid 101 | } 102 | 103 | // using iter_mut to mutate values in place 104 | fn using_iter_mut_for_mutation() { 105 | let mut nums = vec![1,2,3,4]; 106 | for num in nums.iter_mut() { 107 | *num = *num + 1; 108 | } 109 | println!("{:?}", nums); 110 | } 111 | 112 | // using enumerate to add an index to each iter_mut 113 | fn using_enumerate_to_index() { 114 | let colors = vec!["red", "green", "blue"]; 115 | for (i, color) in colors.iter().enumerate() { 116 | println!("{}: {}", i, color); 117 | } 118 | } 119 | 120 | struct TimeBomb { 121 | count: u32, 122 | limit: u32, 123 | } 124 | 125 | impl Iterator for TimeBomb { 126 | type Item = u32; 127 | fn next(&mut self) -> Option { 128 | self.count += 1; 129 | if self.count < self.limit { 130 | Some(self.count) 131 | } else { 132 | println!("💥"); 133 | None 134 | } 135 | } 136 | } 137 | 138 | fn using_custom_iterator() { 139 | let mut tb = TimeBomb{ 140 | count: 0, 141 | limit: 10, 142 | }; 143 | while let Some(count) = tb.next() { 144 | println!("{}", count); 145 | } 146 | } 147 | --------------------------------------------------------------------------------