@@ -1545,20 +1545,22 @@ 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{};
1552- {
1553- let _ref_to_val: &Value = &x;
1554- eat(x);
1555- }
1554+ let _ref_to_val: &Value = &x;
1555+ eat(x);
1556+ borrow(_ref_to_val);
15561557}
15571558```
15581559
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:
1560+ Here, the function `eat` takes ownership of `x`. However,
1561+ `x` cannot be moved because the borrow to `_ref_to_val`
1562+ needs to last till the function `borrow`.
1563+ To fix that you can do a few different things:
15621564
15631565* Try to avoid moving the variable.
15641566* Release borrow before move.
@@ -1569,14 +1571,15 @@ Examples:
15691571```
15701572struct Value {}
15711573
1574+ fn borrow(val: &Value) {}
1575+
15721576fn eat(val: &Value) {}
15731577
15741578fn main() {
15751579 let x = Value{};
1576- {
1577- let _ref_to_val: &Value = &x;
1578- eat(&x); // pass by reference, if it's possible
1579- }
1580+ let _ref_to_val: &Value = &x;
1581+ eat(&x); // pass by reference, if it's possible
1582+ borrow(_ref_to_val);
15801583}
15811584```
15821585
@@ -1585,12 +1588,15 @@ Or:
15851588```
15861589struct Value {}
15871590
1591+ fn borrow(val: &Value) {}
1592+
15881593fn eat(val: Value) {}
15891594
15901595fn main() {
15911596 let x = Value{};
15921597 {
15931598 let _ref_to_val: &Value = &x;
1599+ borrow(_ref_to_val);
15941600 }
15951601 eat(x); // release borrow and then move it.
15961602}
@@ -1602,14 +1608,15 @@ Or:
16021608#[derive(Clone, Copy)] // implement Copy trait
16031609struct Value {}
16041610
1611+ fn borrow(val: &Value) {}
1612+
16051613fn eat(val: Value) {}
16061614
16071615fn main() {
16081616 let x = Value{};
1609- {
1610- let _ref_to_val: &Value = &x;
1611- eat(x); // it will be copied here.
1612- }
1617+ let _ref_to_val: &Value = &x;
1618+ eat(x); // it will be copied here.
1619+ borrow(_ref_to_val);
16131620}
16141621```
16151622
0 commit comments