Merge pull request #156 from jonathandturner/first-third-try

Update errors in references chapter
This commit is contained in:
Carol Nichols 2016-07-29 08:53:03 -04:00 committed by GitHub
commit 8ee39b9583

View File

@ -106,10 +106,12 @@ fn change(some_string: &String) {
Heres the error: Heres 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. Were not Just like bindings are immutable by default, so are references. Were not
@ -146,20 +148,16 @@ let r2 = &mut s;
Heres the error: Heres 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
Heres the error: Heres 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 {
Heres the error: Heres the error:
```text ```text
error: missing lifetime specifier [E0106] error[E0106]: missing lifetime specifier
fn dangle() -> &String { --> dangle.rs:5:16
^~~~~~~ |
help: this functions 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 havent learned about yet, This error message refers to a feature we havent learned about yet,