Improve closures_iterators exercise instructions, change the challenge
This commit is contained in:
@@ -2,15 +2,17 @@
|
|||||||
#[allow(unused_mut)]
|
#[allow(unused_mut)]
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
// 1. Create a closure that returns the square of an integer (the number multiplied by itself),
|
// 1. Uncomment the code below. Create a closure that returns the square of an integer (the
|
||||||
// and assign the closure to a variable called "square".
|
// number multiplied by itself), and assign the closure to the "square" variable. Then run the
|
||||||
|
// code and make sure it works.
|
||||||
|
|
||||||
// let square = ...
|
// let square = ...
|
||||||
//println!("5 squared is {}", square(5));
|
// println!("5 squared is {}", square(5));
|
||||||
|
|
||||||
// 2. Uncomment the code below. Finish the .map() by passing it a closure which takes a tuple
|
// 2. Uncomment the code below. Finish the .map() iterator adaptor call by passing it a closure
|
||||||
// of two integers, and returns a tuple with the first integer incremented by 1, and the second
|
// which takes a tuple of two integers as a parameter, and returns a tuple with the first
|
||||||
// integer left alone. For example, (0, 1) should become (1, 1).
|
// 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.
|
||||||
|
|
||||||
// let pairs = vec![(0, 1), (2, 3), (4, 5)];
|
// let pairs = vec![(0, 1), (2, 3), (4, 5)];
|
||||||
// pairs
|
// pairs
|
||||||
@@ -19,10 +21,9 @@ fn main() {
|
|||||||
// .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 the numbers in vector by 3 without
|
// mutable references to multiply each of the values in `numbers` by 3.
|
||||||
// consuming the vector.
|
// Hint 1: You'll need .iter_mut() -- bonus points if you use the shorter, syntactic sugar form!
|
||||||
// Hint 1: You'll probably want to use .iter_mut()
|
// 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 wherever you use it
|
|
||||||
|
|
||||||
// let mut numbers = vec![1, 2, 3, 4];
|
// let mut numbers = vec![1, 2, 3, 4];
|
||||||
// for x in ... {
|
// for x in ... {
|
||||||
@@ -30,6 +31,11 @@ fn main() {
|
|||||||
// }
|
// }
|
||||||
// println!("{:?}", numbers); // should print [3, 6, 9, 12]
|
// println!("{:?}", numbers); // should print [3, 6, 9, 12]
|
||||||
|
|
||||||
|
let mut numbers = vec![1, 2, 3, 4];
|
||||||
|
numbers.iter_mut().for_each(|x| *x *= 3);
|
||||||
|
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()
|
||||||
// - Use .filter() to remove any word that contains the letter "h" -- use .contains()
|
// - Use .filter() to remove any word that contains the letter "h" -- use .contains()
|
||||||
@@ -42,15 +48,9 @@ fn main() {
|
|||||||
// let transformed... // do the stuff here
|
// let transformed... // do the stuff here
|
||||||
// println!("Transformed: {:?}", transformed);
|
// println!("Transformed: {:?}", transformed);
|
||||||
|
|
||||||
// Challenge: Both .iter() and .iter_mut() can be used via shorter "syntactic sugar" in a
|
// Challenge:
|
||||||
// for-loop definition. For example, instead of:
|
|
||||||
//
|
//
|
||||||
// for x in vector.iter() { ... }
|
// - Rewrite the code in #2 as a for loop
|
||||||
//
|
// - Rewrite the code in #3 in functional style (without a for loop). Hint: There are multiple
|
||||||
// you can do:
|
// ways to accomplish this, but they all end with an iterator consumer.
|
||||||
//
|
|
||||||
// for x in &vector { ... }
|
|
||||||
//
|
|
||||||
// Can you figure out how to change .iter_mut() in #3 to the shorter, syntactic sugar form for
|
|
||||||
// mutable references?
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user