mirror of
https://github.com/rust-lang-cn/book-cn.git
synced 2025-01-25 08:28:44 +08:00
Merge pull request #156 from jonathandturner/first-third-try
Update errors in references chapter
This commit is contained in:
commit
8ee39b9583
@ -106,10 +106,12 @@ fn change(some_string: &String) {
|
|||||||
|
|
||||||
Here’s the error:
|
Here’s the error:
|
||||||
|
|
||||||
```bash
|
```text
|
||||||
8:16 error: cannot borrow immutable borrowed content `*some_string` as mutable
|
error: cannot borrow immutable borrowed content `*some_string` as mutable
|
||||||
some_string.push_str(", world");
|
--> error.rs:8:5
|
||||||
^~~~~~~~~~~
|
|
|
||||||
|
8 | some_string.push_str(", world");
|
||||||
|
| ^^^^^^^^^^^
|
||||||
```
|
```
|
||||||
|
|
||||||
Just like bindings are immutable by default, so are references. We’re not
|
Just like bindings are immutable by default, so are references. We’re not
|
||||||
@ -146,20 +148,16 @@ let r2 = &mut s;
|
|||||||
|
|
||||||
Here’s the error:
|
Here’s the error:
|
||||||
|
|
||||||
```bash
|
```text
|
||||||
5:20 error: cannot borrow `s` as mutable more than once at a time [E0499]
|
error[E0499]: cannot borrow `s` as mutable more than once at a time
|
||||||
let r2 = &mut s;
|
--> borrow_twice.rs:5:19
|
||||||
^
|
|
|
||||||
4:20 note: previous borrow of `s` occurs here; the mutable borrow prevents
|
4 | let r1 = &mut s;
|
||||||
subsequent moves, borrows, or modification of `s` until the borrow
|
| - first mutable borrow occurs here
|
||||||
ends
|
5 | let r2 = &mut s;
|
||||||
let r1 = &mut s;
|
| ^ second mutable borrow occurs here
|
||||||
^
|
6 | }
|
||||||
7:2 note: previous borrow ends here
|
| - first borrow ends here
|
||||||
fn main() {
|
|
||||||
|
|
||||||
}
|
|
||||||
^
|
|
||||||
```
|
```
|
||||||
|
|
||||||
The error is what it says: you cannot borrow something mutably more than once
|
The error is what it says: you cannot borrow something mutably more than once
|
||||||
@ -195,20 +193,16 @@ let r3 = &mut s; // BIG PROBLEM
|
|||||||
Here’s the error:
|
Here’s the error:
|
||||||
|
|
||||||
```text
|
```text
|
||||||
19: 6:20 error: cannot borrow `s` as mutable because it is also borrowed as
|
error[E0502]: cannot borrow `s` as mutable because it is also borrowed as immutable
|
||||||
immutable [E0502]
|
--> borrow_thrice.rs:6:19
|
||||||
let r3 = &mut s; // BIG PROBLEM
|
|
|
||||||
^
|
4 | let r1 = &s; // no problem
|
||||||
15: 4:16 note: previous borrow of `s` occurs here; the immutable borrow
|
| - immutable borrow occurs here
|
||||||
prevents subsequent moves or mutable borrows of `s` until the
|
5 | let r2 = &s; // no problem
|
||||||
borrow ends
|
6 | let r3 = &mut s; // BIG PROBLEM
|
||||||
let r1 = &s; // no problem
|
| ^ mutable borrow occurs here
|
||||||
^
|
7 | }
|
||||||
8:2 note: previous borrow ends here
|
| - immutable borrow ends here
|
||||||
fn main() {
|
|
||||||
|
|
||||||
}
|
|
||||||
^
|
|
||||||
```
|
```
|
||||||
|
|
||||||
Whew! We _also_ cannot have a mutable reference while we have an immutable one.
|
Whew! We _also_ cannot have a mutable reference while we have an immutable one.
|
||||||
@ -240,12 +234,17 @@ fn dangle() -> &String {
|
|||||||
Here’s the error:
|
Here’s the error:
|
||||||
|
|
||||||
```text
|
```text
|
||||||
error: missing lifetime specifier [E0106]
|
error[E0106]: missing lifetime specifier
|
||||||
fn dangle() -> &String {
|
--> dangle.rs:5:16
|
||||||
^~~~~~~
|
|
|
||||||
help: this function’s return type contains a borrowed value, but there is no
|
5 | fn dangle() -> &String {
|
||||||
value for it to be borrowed from
|
| ^^^^^^^
|
||||||
help: consider giving it a 'static lifetime
|
|
|
||||||
|
= help: this function's return type contains a borrowed value, but there is no
|
||||||
|
value for it to be borrowed from
|
||||||
|
= help: consider giving it a 'static lifetime
|
||||||
|
|
||||||
|
error: aborting due to previous error
|
||||||
```
|
```
|
||||||
|
|
||||||
This error message refers to a feature we haven’t learned about yet,
|
This error message refers to a feature we haven’t learned about yet,
|
||||||
|
Loading…
Reference in New Issue
Block a user