@@ -1545,20 +1545,24 @@ Erroneous code example:
15451545```compile_fail,E0505
15461546struct Value {}
15471547
1548+ fn borrow(val: &Value) {}
1549+
15481550fn eat(val: Value) {}
15491551
15501552fn main() {
15511553 let x = Value{};
15521554 {
15531555 let _ref_to_val: &Value = &x;
15541556 eat(x);
1557+ borrow(_ref_to_val);
15551558 }
15561559}
15571560```
15581561
1559- Here, the function `eat` takes the ownership of `x`. However,
1560- `x` cannot be moved because it was borrowed to `_ref_to_val`.
1561- To fix that you can do few different things:
1562+ Here, the function `eat` takes ownership of `x`. However,
1563+ `x` cannot be moved because the borrow to `_ref_to_val`
1564+ needs to last till the function `borrow`.
1565+ To fix that you can do a few different things:
15621566
15631567* Try to avoid moving the variable.
15641568* Release borrow before move.
@@ -1569,13 +1573,16 @@ Examples:
15691573```
15701574struct Value {}
15711575
1576+ fn borrow(val: &Value) {}
1577+
15721578fn eat(val: &Value) {}
15731579
15741580fn main() {
15751581 let x = Value{};
15761582 {
15771583 let _ref_to_val: &Value = &x;
15781584 eat(&x); // pass by reference, if it's possible
1585+ borrow(_ref_to_val);
15791586 }
15801587}
15811588```
@@ -1585,12 +1592,15 @@ Or:
15851592```
15861593struct Value {}
15871594
1595+ fn borrow(val: &Value) {}
1596+
15881597fn eat(val: Value) {}
15891598
15901599fn main() {
15911600 let x = Value{};
15921601 {
15931602 let _ref_to_val: &Value = &x;
1603+ borrow(_ref_to_val);
15941604 }
15951605 eat(x); // release borrow and then move it.
15961606}
@@ -1602,13 +1612,16 @@ Or:
16021612#[derive(Clone, Copy)] // implement Copy trait
16031613struct Value {}
16041614
1615+ fn borrow(val: &Value) {}
1616+
16051617fn eat(val: Value) {}
16061618
16071619fn main() {
16081620 let x = Value{};
16091621 {
16101622 let _ref_to_val: &Value = &x;
16111623 eat(x); // it will be copied here.
1624+ borrow(_ref_to_val);
16121625 }
16131626}
16141627```
0 commit comments