diff options
| author | Kaiyin Zhong <kindlychung@gmail.com> | 2016-04-26 17:40:59 +0200 |
|---|---|---|
| committer | Kaiyin Zhong <kindlychung@gmail.com> | 2016-04-26 17:40:59 +0200 |
| commit | 10abb666e48abdb747946db6de21317708e18cf5 (patch) | |
| tree | 30104e022a7cd5db89e755c863717d061344abd7 | |
| parent | a3f5d8aea1e43a1c5b0814dac407e6ddfab07417 (diff) | |
| download | rust-10abb666e48abdb747946db6de21317708e18cf5.tar.gz rust-10abb666e48abdb747946db6de21317708e18cf5.zip | |
Update references-and-borrowing.md
add as 2nd example.
| -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 { |
