X-Git-Url: https://git.ralfj.de/rust-101.git/blobdiff_plain/b60c82e9d3b03aa36484c1ff68f34f4e78862d46..4fcb5d41bd72739b76beac0466a2dd59e403138b:/src/part06.rs?ds=inline diff --git a/src/part06.rs b/src/part06.rs index 5b90142..8161d2d 100644 --- a/src/part06.rs +++ b/src/part06.rs @@ -26,11 +26,11 @@ impl BigInt { } // Now we can write `vec_min`. -//@ However, in order to make it type-check, we have to make a full (deep) copy of e by calling `clone`. fn vec_min(v: &Vec) -> Option { let mut min: Option = None; + // If `v` is a shared borrowed vector, then the default for iterating over it is to call `iter`, the iterator that borrows the elements. for e in v { - let e = e.clone(); /*@*/ + let e = e.clone(); min = Some(match min { /*@*/ None => e, /*@*/ Some(n) => e.min_try1(n) /*@*/ @@ -38,7 +38,7 @@ fn vec_min(v: &Vec) -> Option { } min } -//@ Now, what's happening here? Why do we have to clone `e`, and why did we not +//@ Now, what's happening here? Why do we have to to make a full (deep) copy of `e`, and why did we not //@ have to do that in our previous version? //@ //@ The answer is already hidden in the type of `vec_min`: `v` is just borrowed, but @@ -50,7 +50,7 @@ fn vec_min(v: &Vec) -> Option { //@ underlying data is transferred from where `e` borrows from to `min`. But that's not allowed, since //@ we just borrowed `e`, so we cannot empty it! We can, however, call `clone` on it. Then we own //@ the copy that was created, and hence we can store it in `min`.
-//@ Of course, making such a full copy is expensive, so we'd like to avoid it. We'll some to that in the next part. +//@ Of course, making such a full copy is expensive, so we'd like to avoid it. We'll come to that in the next part. // ## `Copy` types //@ But before we go there, I should answer the second question I brought up above: Why did our old `vec_min` work?