Skip to content

Remove outdated error while explicitly dropping a future with drop #799

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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