From 3712439caf5e469a5cccc22eb5f4cb0a854be379 Mon Sep 17 00:00:00 2001 From: jayber Date: Tue, 7 Jun 2022 13:39:54 +0100 Subject: [PATCH] feat: improved closure docs and exercises by using into_iter() --- exercises/closures/closures5.rs | 3 +++ exercises/closures/closures7.rs | 2 +- info.toml | 8 +++++--- 3 files changed, 9 insertions(+), 4 deletions(-) diff --git a/exercises/closures/closures5.rs b/exercises/closures/closures5.rs index d4dddf62..4f97d735 100644 --- a/exercises/closures/closures5.rs +++ b/exercises/closures/closures5.rs @@ -21,6 +21,9 @@ fn sum_letters(animals: &Vec<&str>) -> usize { // pay close attention to the where clause in the function signatures // https://doc.rust-lang.org/stable/std/iter/trait.Iterator.html#method.reduce // https://doc.rust-lang.org/stable/std/iter/trait.Iterator.html#method.fold + // and the return types of the different ways of getting an iterator + // https://doc.rust-lang.org/stable/std/iter/ + // https://doc.rust-lang.org/std/iter/trait.IntoIterator.html#tymethod.into_iter // TODO: change the next 2 lines to compile and pass the test. let sum_closure = |x: &usize, y: &usize| &(x + y); animals.iter().reduce(sum_closure).unwrap().to_owned() diff --git a/exercises/closures/closures7.rs b/exercises/closures/closures7.rs index 555aa39c..c0d73b2a 100644 --- a/exercises/closures/closures7.rs +++ b/exercises/closures/closures7.rs @@ -20,7 +20,7 @@ fn main() { let maybe = Some("yes"); let list = vec![maybe]; - list.iter().inspect(|&Some(value)| { + list.into_iter().inspect(|Some(value)| { println!("maybe {:?}", value); }); } \ No newline at end of file diff --git a/info.toml b/info.toml index 6ea0defc..555b2076 100644 --- a/info.toml +++ b/info.toml @@ -178,13 +178,15 @@ path = "exercises/closures/closures5.rs" mode = "test" hint = """ Getting the signatures of closures right can be tough. I don't think you can write -any version that fulfills the tests using 'reduce()': you can never return a borrow +any version that fulfills the tests using 'reduce()' with 'iter()': you can never return a borrow of a value owned by a called function. -You need to replace 'reduce()' with a different function call, update the closure +You can replace 'reduce()' with a different function call. Update the closure so that it matches the signature of this new call, and change the code to return the correct type from the sum_letters function. (Using 'sum()' is cheating!) +OR you can change the sort of iterator you get, so that you don't have to return a reference. Pay close attention to the documentation linked to in the exercise: which type -parameters are the same in the function signatures, and which are different.""" +parameters are the same in the function signatures, and which are different. And +how do the return types of the variations of getting an iterator differ from each other?""" [[exercises]] name = "closures6"