-
-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge question 28: underscore prefixed variables
- Loading branch information
Showing
4 changed files
with
64 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 @@ | ||
../index.html |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,42 @@ | ||
Answer: 3121 | ||
Difficulty: 1 | ||
|
||
# Hint | ||
|
||
A value is dropped when it no longer has an owner. | ||
|
||
# Explanation | ||
|
||
The program prints `3121`. That is, the `Drop` impl for `let _guard = Guard` | ||
runs at the end of main but the `Drop` impl for `let _ = Guard` runs right away. | ||
|
||
In general, a value is dropped when it no longer has an owner. The variable | ||
`_guard` owns the first value of type `Guard` and remains in scope until the end | ||
of main. The `_` is not a variable but a wildcard pattern that binds nothing; | ||
since no variables are bound on this line, there is no variable to be the owner | ||
of the second value of type `Guard` and that value is dropped on the same line. | ||
|
||
This distinction between the underscore pattern vs variables with a leading | ||
underscore is incredibly important to remember when working with lock guards in | ||
unsafe code. | ||
|
||
use lazy_static::lazy_static; | ||
use std::sync::Mutex; | ||
|
||
lazy_static! { | ||
static ref MUTEX: Mutex<()> = Mutex::new(()); | ||
} | ||
|
||
/// MUTEX must be held when accessing this value. | ||
static mut VALUE: usize = 0; | ||
|
||
fn main() { | ||
let _guard = MUTEX.lock().unwrap(); | ||
unsafe { | ||
VALUE += 1; | ||
} | ||
} | ||
|
||
If this code were to use `let _ = MUTEX.lock().unwrap()` then the mutex guard | ||
would be dropped immediately, releasing the mutex and failing to guard the | ||
access of `VALUE`. |
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,14 @@ | ||
struct Guard; | ||
|
||
impl Drop for Guard { | ||
fn drop(&mut self) { | ||
print!("1"); | ||
} | ||
} | ||
|
||
fn main() { | ||
let _guard = Guard; | ||
print!("3"); | ||
let _ = Guard; | ||
print!("2"); | ||
} |