diff options
| -rw-r--r-- | src/doc/book/references-and-borrowing.md | 18 |
1 files changed, 18 insertions, 0 deletions
diff --git a/src/doc/book/references-and-borrowing.md b/src/doc/book/references-and-borrowing.md index 3a1f3f004be..67a8a82f2a3 100644 --- a/src/doc/book/references-and-borrowing.md +++ b/src/doc/book/references-and-borrowing.md @@ -62,6 +62,24 @@ This is not idiomatic Rust, however, as it doesn’t take advantage of borrowing the first step: ```rust +fn foo(v1: &Vec<i32>, v2: &Vec<i32>) -> i32 { + // do stuff with v1 and v2 + + // return the answer + 42 +} + +let v1 = vec![1, 2, 3]; +let v2 = vec![1, 2, 3]; + +let answer = foo(&v1, &v2); + +// we can use v1 and v2 here! +``` + +A more concrete example: + +```rust fn main() { // Don't worry if you don't understand how `fold` works, the point here is that an immutable reference is borrowed. fn sum_vec(v: &Vec<i32>) -> i32 { |
