-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
It's a fairly easy one, but interesting nevertheless.
- Loading branch information
Showing
4 changed files
with
63 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
type R1<'a> = &'a mut i32; | ||
struct R2<'a>(&'a mut i32); | ||
|
||
fn r1_once(_: R1){} | ||
fn r2_once(_: R2){} | ||
|
||
pub fn r1_twice(x: R1) { | ||
r1_once(x); | ||
r1_once(x); | ||
} | ||
|
||
pub fn r2_twice(x: R2) { | ||
r2_once(x); | ||
r2_once(x); | ||
} | ||
|
||
fn main() { | ||
r1_twice(&mut 0); | ||
r2_twice(R2(&mut 0)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
error[E0382]: use of moved value: `x` | ||
--> examples/borrowck_6.rs:14:13 | ||
| | ||
12 | pub fn r2_twice(x: R2) { | ||
| - move occurs because `x` has type `R2<'_>`, which does not implement the `Copy` trait | ||
13 | r2_once(x); | ||
| - value moved here | ||
14 | r2_once(x); | ||
| ^ value used here after move | ||
| | ||
note: consider changing this parameter type in function `r2_once` to borrow instead if owning the value isn't necessary | ||
--> examples/borrowck_6.rs:5:15 | ||
| | ||
5 | fn r2_once(_: R2){} | ||
| ------- ^^ this parameter takes ownership of the value | ||
| | | ||
| in this function | ||
|
||
For more information about this error, try `rustc --explain E0382`. | ||
error: could not compile `code` (example "borrowck_6") due to 1 previous error |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# Borrowck 6 @adotinthevoid @Nilstrieb | ||
|
||
{{#include ../include/quiz-is-wip.md}} | ||
|
||
```rust | ||
{{#include ../../code/examples/borrowck_6.rs}} | ||
``` | ||
|
||
<details> | ||
<summary>Solution</summary> | ||
|
||
``` | ||
{{#include ../../code/examples/stderr/borrowck_6.stderr}} | ||
``` | ||
|
||
`r2_twice` does not compile because `R2` does not implement `Copy`, and is moved twice. | ||
This is basic Rust, but `r1_twice` *does* compile even though `&mut i32` does *not* implement `Copy`! | ||
That's because for `R1`, the reference is not moved into `r1_once`, it is instead *reborrowed*. | ||
Reborrows are a special operation on references that the borrow checker understands and behave like `&mut *x`. | ||
So there is just a new reference passed to `r1_once`, with the original one preserved. | ||
|
||
</details> |