1- This error indicates that a temporary value is being dropped
2- while a borrow is still in active use.
1+ A temporary value is being dropped while a borrow is still in active use.
32
43Erroneous code example:
54
@@ -11,12 +10,11 @@ let p = bar(&foo());
1110let q = *p;
1211```
1312
14- Here, the expression ` &foo() ` is borrowing the expression
15- ` foo() ` . As ` foo() ` is a call to a function, and not the name of
16- a variable, this creates a ** temporary** -- that temporary stores
17- the return value from ` foo() ` so that it can be borrowed.
18- You could imagine that ` let p = bar(&foo()); ` is equivalent
19- to this:
13+ Here, the expression ` &foo() ` is borrowing the expression ` foo() ` . As ` foo() ` is
14+ a call to a function, and not the name of a variable, this creates a
15+ ** temporary** -- that temporary stores the return value from ` foo() ` so that it
16+ can be borrowed. You could imagine that ` let p = bar(&foo()); ` is equivalent to
17+ this:
2018
2119``` compile_fail,E0597
2220# fn foo() -> i32 { 22 }
@@ -28,16 +26,14 @@ let p = {
2826let q = p;
2927```
3028
31- Whenever a temporary is created, it is automatically dropped (freed)
32- according to fixed rules. Ordinarily, the temporary is dropped
33- at the end of the enclosing statement -- in this case, after the ` let ` .
34- This is illustrated in the example above by showing that ` tmp ` would
35- be freed as we exit the block.
29+ Whenever a temporary is created, it is automatically dropped (freed) according
30+ to fixed rules. Ordinarily, the temporary is dropped at the end of the enclosing
31+ statement -- in this case, after the ` let ` . This is illustrated in the example
32+ above by showing that ` tmp ` would be freed as we exit the block.
3633
37- To fix this problem, you need to create a local variable
38- to store the value in rather than relying on a temporary.
39- For example, you might change the original program to
40- the following:
34+ To fix this problem, you need to create a local variable to store the value in
35+ rather than relying on a temporary. For example, you might change the original
36+ program to the following:
4137
4238```
4339fn foo() -> i32 { 22 }
@@ -47,16 +43,15 @@ let p = bar(&value);
4743let q = *p;
4844```
4945
50- By introducing the explicit ` let value ` , we allocate storage
51- that will last until the end of the enclosing block (when ` value `
52- goes out of scope). When we borrow ` &value ` , we are borrowing a
53- local variable that already exists, and hence no temporary is created.
46+ By introducing the explicit ` let value ` , we allocate storage that will last
47+ until the end of the enclosing block (when ` value ` goes out of scope). When we
48+ borrow ` &value ` , we are borrowing a local variable that already exists, and
49+ hence no temporary is created.
5450
55- Temporaries are not always dropped at the end of the enclosing
56- statement. In simple cases where the ` & ` expression is immediately
57- stored into a variable, the compiler will automatically extend
58- the lifetime of the temporary until the end of the enclosing
59- block. Therefore, an alternative way to fix the original
51+ Temporaries are not always dropped at the end of the enclosing statement. In
52+ simple cases where the ` & ` expression is immediately stored into a variable, the
53+ compiler will automatically extend the lifetime of the temporary until the end
54+ of the enclosing block. Therefore, an alternative way to fix the original
6055program is to write ` let tmp = &foo() ` and not ` let tmp = foo() ` :
6156
6257```
@@ -67,10 +62,10 @@ let p = bar(value);
6762let q = *p;
6863```
6964
70- Here, we are still borrowing ` foo() ` , but as the borrow is assigned
71- directly into a variable, the temporary will not be dropped until
72- the end of the enclosing block. Similar rules apply when temporaries
73- are stored into aggregate structures like a tuple or struct:
65+ Here, we are still borrowing ` foo() ` , but as the borrow is assigned directly
66+ into a variable, the temporary will not be dropped until the end of the
67+ enclosing block. Similar rules apply when temporaries are stored into aggregate
68+ structures like a tuple or struct:
7469
7570```
7671// Here, two temporaries are created, but
0 commit comments