Iterators |
Description
an iterator helps to iterate over a collection of values such as arrays, vectors, maps, etc.iterators implement the Iterator trait that is defined in the Rust standard library iter() method returns an iterator object of the collection values in an iterator object are called items next() method of the iterator can be used to traverse through the items next() method returns a value None when it reaches the end of the collection example uses an iterator to read values from an array fn main() { //declare an array let a = [10,20,30]; let mut iter = a.iter(); // fetch an iterator object for the array println!("{:?}",iter); //fetch individual values from the iterator object println!("{:?}",iter.next()); println!("{:?}",iter.next()); println!("{:?}",iter.next()); println!("{:?}",iter.next()); }output Iter([10, 20, 30]) Some(10) Some(20) Some(30) Noneany type implementing the Iterator trait, it can be traversed using the for...in syntax fn main() { let a = [10,20,30]; let iter = a.iter(); for data in iter{ print!("{}\t",data); } } iter()
iter() gives an iterator over &T(reference to T)The iter() function uses the concept of borrowing returns a reference to each element of the collection leaves the collection untouched and available for reuse after the loop fn main() { let names = vec!["Kannan", "Mohtashim", "Kiran"]; for name in names.iter() { match name { &"Mohtashim" => println!("There is a rustacean among us!"), // the underscore means that the compiler can infer the name/type // in match statement it is the equivalent to default _ => println!("Hello {}", name), } } println!("{:?}",names); // reusing the collection after iteration }output Hello Kannan There is a rustacean among us! Hello Kiran ["Kannan", "Mohtashim", "Kiran"] into_iter()
into_iter() gives an iterator over Tfunction uses the concept of ownership moves values in the collection into an iter object the collection is consumed and it is no longer available for reuse fn main(){ let names = vec!["Kannan", "Mohtashim", "Kiran"]; for name in names.into_iter() { match name { "Mohtashim" => println!("There is a rustacean among us!"), _ => println!("Hello {}", name), } } // cannot reuse the collection after iteration println!("{:?}",names); // Error:Cannot access after ownership move } for and iter_mut()
iter_mut() gives an iterator over &mut Tfunction is like the iter() function function can modify elements within the collection fn main() { let mut names = vec!["Kannan", "Mohtashim", "Kiran"]; for name in names.iter_mut() { match name { &mut "Mohtashim" => println!("There is a rustacean among us!"), _ => println!("Hello {}", name), } } println!("{:?}",names); // reusing the collection after iteration }output Hello Kannan There is a rustacean among us! Hello Kiran ["Kannan", "Mohtashim", "Kiran"] |
Closures |
Closure refers to a function within another function are anonymous functions – functions without a name Closure can be used to assign a function to a variable allows a program to pass a function as a parameter to other functions Closure is also known as an inline function variables in the outer function can be accessed by inline functions Defining a Closure
a closure definition may optionally have parametersparameters are enclosed within two vertical bars let closure_function = |parameter| { //logic }syntax invoking a Closure implements Fn traits it can be invoked with () syntax closure_function(parameter); //invokingexample defines a closure is_even within the function main() returns true if a number is even and returns false if the number is odd fn main(){ let is_even = |x| { x%2==0 }; let no = 13; println!("{} is even ? {}",no,is_even(no)); } Closure using outer function variable
main() function declares a variable val and a closurethe closure accesses the variable declared in the outer function main(). fn main(){ let val = 10; // declared outside let closure2 = |x| { x + val //inner function accessing outer fn variable }; println!("{}",closure2(2)); } |