Rollup merge of #25222 - GuillaumeGomez:doc-ref, r=steveklabnik
r? @steveklabnik
This commit is contained in:
commit
ba8eb58257
1 changed files with 35 additions and 0 deletions
|
|
@ -312,6 +312,7 @@ println!("{}", y);
|
|||
|
||||
We get this error:
|
||||
|
||||
```text
|
||||
error: `x` does not live long enough
|
||||
y = &x;
|
||||
^
|
||||
|
|
@ -334,3 +335,37 @@ In other words, `y` is only valid for the scope where `x` exists. As soon as
|
|||
`x` goes away, it becomes invalid to refer to it. As such, the error says that
|
||||
the borrow ‘doesn’t live long enough’ because it’s not valid for the right
|
||||
amount of time.
|
||||
|
||||
The same problem occurs when the reference is declared _before_ the variable it refers to:
|
||||
|
||||
```rust,ignore
|
||||
let y: &i32;
|
||||
let x = 5;
|
||||
y = &x;
|
||||
|
||||
println!("{}", y);
|
||||
```
|
||||
|
||||
We get this error:
|
||||
|
||||
```text
|
||||
error: `x` does not live long enough
|
||||
y = &x;
|
||||
^
|
||||
note: reference must be valid for the block suffix following statement 0 at
|
||||
2:16...
|
||||
let y: &i32;
|
||||
let x = 5;
|
||||
y = &x;
|
||||
|
||||
println!("{}", y);
|
||||
}
|
||||
|
||||
note: ...but borrowed value is only valid for the block suffix following
|
||||
statement 1 at 3:14
|
||||
let x = 5;
|
||||
y = &x;
|
||||
|
||||
println!("{}", y);
|
||||
}
|
||||
```
|
||||
Loading…
Add table
Add a link
Reference in a new issue