*oops* make things actually compile
authorRalf Jung <post@ralfj.de>
Thu, 9 Jul 2015 15:55:00 +0000 (17:55 +0200)
committerRalf Jung <post@ralfj.de>
Thu, 9 Jul 2015 15:55:24 +0000 (17:55 +0200)
TODO.txt
src/part10.rs
workspace/src/part10.rs

index 73c82b1885fe4030843cf7de8d8dab1705ba2160..1bf2f510484fd3d4b094501f0f8031487f352762 100644 (file)
--- a/TODO.txt
+++ b/TODO.txt
@@ -1,5 +1,3 @@
-* Closures: Working on iterators
-
 * Arrays/slices
 * Trait objects
 
index e0481a89120ef41e045f420d915666df7f6bd30d..c8a0129a5ad4f4508091dcd449bab87ad89323fc 100644 (file)
@@ -132,8 +132,9 @@ fn print_enumerated<T: fmt::Display>(v: &Vec<T>) {
 // 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<i32>, divisor: i32) -> Vec<i32> {
     //@ 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
index 9fd76422d5e3a756343d3e7409004cfb8a56319b..f7d9028ad46a15e9ccc4a0b11fca8b1d84584b28 100644 (file)
@@ -90,7 +90,7 @@ fn print_enumerated<T: fmt::Display>(v: &Vec<T>) {
 
 // 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<i32>, divisor: i32) -> Vec<i32> {
-    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