From: Ralf Jung Date: Thu, 9 Jul 2015 15:55:00 +0000 (+0200) Subject: *oops* make things actually compile X-Git-Url: https://git.ralfj.de/rust-101.git/commitdiff_plain/942f0abd4b0931acad2d3def58cb7273ace15e2a?ds=sidebyside *oops* make things actually compile --- diff --git a/TODO.txt b/TODO.txt index 73c82b1..1bf2f51 100644 --- a/TODO.txt +++ b/TODO.txt @@ -1,5 +1,3 @@ -* Closures: Working on iterators - * Arrays/slices * Trait objects diff --git a/src/part10.rs b/src/part10.rs index e0481a8..c8a0129 100644 --- a/src/part10.rs +++ b/src/part10.rs @@ -132,8 +132,9 @@ fn print_enumerated(v: &Vec) { // And as a final example, one can also collect all elements of an iterator, and put them, e.g., in a vector. fn filter_vec_by_divisor(v: &Vec, divisor: i32) -> Vec { //@ Here, the return type of `collect` is inferred based on the return type of our function. In general, it can return anything implementing - //@ [`FromIterator`](http://doc.rust-lang.org/stable/std/iter/trait.FromIterator.html). - v.iter().filter(|n| *n % divisor == 0).collect() + //@ [`FromIterator`](http://doc.rust-lang.org/stable/std/iter/trait.FromIterator.html). Notice that `iter` gives us an iterator over + //@ borrowed `i32`, but we want to own them for the result, so we insert a `map` to dereference. + v.iter().map(|n| *n).filter(|n| *n % divisor == 0).collect() } // **Exercise 10.1**: Look up the [documentation of `Iterator`](http://doc.rust-lang.org/stable/std/iter/trait.Iterator.html) to learn about more functions diff --git a/workspace/src/part10.rs b/workspace/src/part10.rs index 9fd7642..f7d9028 100644 --- a/workspace/src/part10.rs +++ b/workspace/src/part10.rs @@ -90,7 +90,7 @@ fn print_enumerated(v: &Vec) { // And as a final example, one can also collect all elements of an iterator, and put them, e.g., in a vector. fn filter_vec_by_divisor(v: &Vec, divisor: i32) -> Vec { - v.iter().filter(|n| *n % divisor == 0).collect() + v.iter().map(|n| *n).filter(|n| *n % divisor == 0).collect() } // **Exercise 10.1**: Look up the [documentation of `Iterator`](http://doc.rust-lang.org/stable/std/iter/trait.Iterator.html) to learn about more functions