Update references-and-borrowing.md

add as 2nd example.
This commit is contained in:
Kaiyin Zhong 2016-04-26 17:40:59 +02:00
parent a3f5d8aea1
commit 10abb666e4

View File

@ -61,6 +61,24 @@ let (v1, v2, answer) = foo(v1, v2);
This is not idiomatic Rust, however, as it doesnt take advantage of borrowing. Heres
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.