make the borrowing example more concrete
This commit is contained in:
parent
74b3684d00
commit
a3f5d8aea1
1 changed files with 19 additions and 11 deletions
|
|
@ -62,19 +62,27 @@ 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
|
||||
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 {
|
||||
return v.iter().fold(0, |a, &b| a + b);
|
||||
}
|
||||
// Borrow two vectors and and sum them.
|
||||
// This kind of borrowing does not allow mutation to the borrowed.
|
||||
fn foo(v1: &Vec<i32>, v2: &Vec<i32>) -> i32 {
|
||||
// do stuff with v1 and v2
|
||||
let s1 = sum_vec(v1);
|
||||
let s2 = sum_vec(v2);
|
||||
// return the answer
|
||||
s1 + s2
|
||||
}
|
||||
|
||||
// return the answer
|
||||
42
|
||||
let v1 = vec![1, 2, 3];
|
||||
let v2 = vec![4, 5, 6];
|
||||
|
||||
let answer = foo(&v1, &v2);
|
||||
println!("{}", answer);
|
||||
}
|
||||
|
||||
let v1 = vec![1, 2, 3];
|
||||
let v2 = vec![1, 2, 3];
|
||||
|
||||
let answer = foo(&v1, &v2);
|
||||
|
||||
// we can use v1 and v2 here!
|
||||
```
|
||||
|
||||
Instead of taking `Vec<i32>`s as our arguments, we take a reference:
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue