From: Mattia Pitossi Date: Thu, 16 Oct 2025 15:13:16 +0000 (+0200) Subject: task: add example for `spawn_local` usage on local runtime (#7689) X-Git-Url: https://git.feebdaed.xyz/?a=commitdiff_plain;h=444d3f5c49ebc8449ba4feccae5bb1a740313953;p=0xmirror%2Ftokio.git task: add example for `spawn_local` usage on local runtime (#7689) --- diff --git a/tokio/src/task/local.rs b/tokio/src/task/local.rs index 5de3dc9e..021e6277 100644 --- a/tokio/src/task/local.rs +++ b/tokio/src/task/local.rs @@ -353,6 +353,8 @@ cfg_rt! { /// /// # Examples /// + /// With `LocalSet`: + /// /// ```rust /// use std::rc::Rc; /// use tokio::task; @@ -373,10 +375,27 @@ cfg_rt! { /// }).await; /// # } /// ``` + /// With local runtime flavor ([Unstable API][unstable] only). + /// + /// ```rust + /// # #[cfg(tokio_unstable)] + /// #[tokio::main(flavor = "local")] + /// async fn main() { + /// let join = tokio::task::spawn_local(async { + /// println!("my nonsend data...") + /// }); + /// + /// join.await.unwrap() + /// } + /// # #[cfg(not(tokio_unstable))] + /// # fn main() {} + /// + /// ``` /// /// [`LocalSet`]: struct@crate::task::LocalSet /// [`LocalRuntime`]: struct@crate::runtime::LocalRuntime /// [`tokio::spawn`]: fn@crate::task::spawn + /// [unstable]: ../../tokio/index.html#unstable-features #[track_caller] pub fn spawn_local(future: F) -> JoinHandle where