Complete exercise: closures_iterators
This commit is contained in:
@@ -6,30 +6,31 @@ fn main() {
|
|||||||
// number multiplied by itself), and assign the closure to the "square" variable. Then run the
|
// number multiplied by itself), and assign the closure to the "square" variable. Then run the
|
||||||
// code and make sure it works.
|
// code and make sure it works.
|
||||||
|
|
||||||
// let square = ...
|
let square = |x| x * x;
|
||||||
// println!("5 squared is {}", square(5));
|
println!("5 squared is {}", square(5));
|
||||||
|
|
||||||
// 2. Uncomment the code below. Finish the .map() iterator adaptor call by passing it a closure
|
// 2. Uncomment the code below. Finish the .map() iterator adaptor call by passing it a closure
|
||||||
// which takes a tuple of two integers as a parameter, and returns a tuple with the first
|
// which takes a tuple of two integers as a parameter, and returns a tuple with the first
|
||||||
// integer incremented by 1, and the second integer left alone. For example, if given the input
|
// integer incremented by 1, and the second integer left alone. For example, if given the input
|
||||||
// (0, 1), it should return (1, 1). Run the code and make sure it works.
|
// (0, 1), it should return (1, 1). Run the code and make sure it works.
|
||||||
|
|
||||||
// let pairs = vec![(0, 1), (2, 3), (4, 5)];
|
let pairs = vec![(0, 1), (2, 3), (4, 5)];
|
||||||
// pairs
|
pairs
|
||||||
// .into_iter()
|
.into_iter()
|
||||||
// .map( ... )
|
.map(|t| (t.0 + 1, t.1))
|
||||||
// .for_each(|t| println!("{:?}", t));
|
.for_each(|t| println!("{:?}", t));
|
||||||
|
|
||||||
// 3. Uncomment the code below. There is a mutable vector named `numbers`. Use an iterator over
|
// 3. Uncomment the code below. There is a mutable vector named `numbers`. Use an iterator over
|
||||||
// mutable references to multiply each of the values in `numbers` by 3.
|
// mutable references to multiply each of the values in `numbers` by 3.
|
||||||
// Hint 1: You'll need .iter_mut() -- bonus points if you use the shorter, syntactic sugar form!
|
// Hint 1: You'll need .iter_mut() -- bonus points if you use the shorter, syntactic sugar form!
|
||||||
// Hint 2: `x` will be a mutable reference, so remember to dereference it to use it
|
// Hint 2: `x` will be a mutable reference, so remember to dereference it to use it
|
||||||
|
|
||||||
// let mut numbers = vec![1, 2, 3, 4];
|
let mut numbers = vec![1, 2, 3, 4];
|
||||||
// for x in ... {
|
// numbers = numbers.iter_mut().map(|x| *x * 3).collect();
|
||||||
// ... // multiply the value by 3 via the mutable reference x
|
for n in &mut numbers {
|
||||||
// }
|
*n = *n * 3;
|
||||||
// println!("{:?}", numbers); // should print [3, 6, 9, 12]
|
}
|
||||||
|
println!("{:?}", numbers); // should print [3, 6, 9, 12]
|
||||||
|
|
||||||
// 4. Uncomment the code below. Take the vector of words and
|
// 4. Uncomment the code below. Take the vector of words and
|
||||||
// - Convert the vector into an iterator with .into_iter()
|
// - Convert the vector into an iterator with .into_iter()
|
||||||
@@ -39,9 +40,9 @@ fn main() {
|
|||||||
//
|
//
|
||||||
// Hint: .to_uppercase() is a method on `str` which returns a String
|
// Hint: .to_uppercase() is a method on `str` which returns a String
|
||||||
|
|
||||||
// let words = vec!["autobot", "beach", "car", "decepticon", "energon", "frothy"];
|
let words = vec!["autobot", "beach", "car", "decepticon", "energon", "frothy"];
|
||||||
// let transformed... // do the stuff here
|
let transformed = words.into_iter().filter(|w| !w.contains("h")).map(|w| w.to_uppercase()).collect::<Vec<String>>();
|
||||||
// println!("Transformed: {:?}", transformed);
|
println!("Transformed: {:?}", transformed);
|
||||||
|
|
||||||
// Challenge:
|
// Challenge:
|
||||||
//
|
//
|
||||||
|
|||||||
Reference in New Issue
Block a user