From 0cb7e416fddb9af0992e23c166717ac3bbbeab97 Mon Sep 17 00:00:00 2001 From: Bogdan Arabadzhi <8407846+barabadzhi@users.noreply.github.com> Date: Wed, 20 Nov 2024 10:08:04 +0100 Subject: [PATCH] Remove outdated error while explicitly dropping a future with drop Signed-off-by: Bogdan Arabadzhi <8407846+barabadzhi@users.noreply.github.com> --- content/tokio/tutorial/shared-state.md | 23 ++++++----------------- 1 file changed, 6 insertions(+), 17 deletions(-) diff --git a/content/tokio/tutorial/shared-state.md b/content/tokio/tutorial/shared-state.md index 7be04ce1..0f1cd6e3 100644 --- a/content/tokio/tutorial/shared-state.md +++ b/content/tokio/tutorial/shared-state.md @@ -205,28 +205,17 @@ async fn increment_and_do_stuff(mutex: &Mutex) { *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) { - let mut lock: MutexGuard = 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 = 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].