Update references-and-borrowing.md
add as 2nd example.
This commit is contained in:
parent
a3f5d8aea1
commit
10abb666e4
@ -61,6 +61,24 @@ let (v1, v2, answer) = foo(v1, v2);
|
||||
This is not idiomatic Rust, however, as it doesn’t take advantage of borrowing. Here’s
|
||||
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.
|
||||
|
Loading…
x
Reference in New Issue
Block a user