Skip to content

Commit

Permalink
Remove outdated error while explicitly dropping a future with drop
Browse files Browse the repository at this point in the history
Signed-off-by: Bogdan Arabadzhi <[email protected]>
  • Loading branch information
barabadzhi committed Nov 20, 2024
1 parent 14cdcc0 commit 0cb7e41
Showing 1 changed file with 6 additions and 17 deletions.
23 changes: 6 additions & 17 deletions content/tokio/tutorial/shared-state.md
Original file line number Diff line number Diff line change
Expand Up @@ -205,28 +205,17 @@ async fn increment_and_do_stuff(mutex: &Mutex<i32>) {
*lock += 1;
} // lock goes out of scope here

do_something_async().await;
}
# async fn do_something_async() {}
```
Note that this does not work:
```rust
use std::sync::{Mutex, MutexGuard};

// This fails too.
async fn increment_and_do_stuff(mutex: &Mutex<i32>) {
let mut lock: MutexGuard<i32> = mutex.lock().unwrap();
*lock += 1;
drop(lock);
// Alternatively, you can use drop(lock) to explicitly drop the lock.
// This does not require a nested scope.
//
// let mut lock: MutexGuard<i32> = mutex.lock().unwrap();
// *lock += 1;
// drop(lock); // lock goes out of scope here

do_something_async().await;
}
# async fn do_something_async() {}
```
This is because the compiler currently calculates whether a future is `Send`
based on scope information only. The compiler will hopefully be updated to
support explicitly dropping it in the future, but for now, you must explicitly
use a scope.

Note that the error discussed here is also discussed in the [Send bound section
from the spawning chapter][send-bound].
Expand Down

0 comments on commit 0cb7e41

Please sign in to comment.