X-Git-Url: https://git.ralfj.de/rust-101.git/blobdiff_plain/bbd22a3d23af1ea8186c7a3bf733a07ba1f8ff70..2022af3d411f12b637e40bcac62b73d97d7fc091:/workspace/src/part04.rs?ds=inline diff --git a/workspace/src/part04.rs b/workspace/src/part04.rs index c7969ac..bde913f 100644 --- a/workspace/src/part04.rs +++ b/workspace/src/part04.rs @@ -23,7 +23,9 @@ fn vec_min(v: &Vec) -> Option { use std::cmp; let mut min = None; - for e in v { + // This time, we explicitly request an iterator for the vector `v`. The method `iter` borrows the vector + // it works on, and provides shared borrows of the elements. + for e in v.iter() { // In the loop, `e` now has type `&i32`, so we have to dereference it to obtain an `i32`. min = Some(match min { None => *e, @@ -45,7 +47,7 @@ fn shared_borrow_demo() { // ## Mutable borrowing fn vec_inc(v: &mut Vec) { - for e in v { + for e in v.iter_mut() { *e += 1; } }