Function tokio::task::spawn_local[][src]

pub fn spawn_local<F>(future: F) -> JoinHandle<F::Output>
Notable traits for JoinHandle<T>
impl<T> Future for JoinHandle<T> type Output = Result<T, JoinError>;
    F: Future + 'static,
    F::Output: 'static, 
This is supported on crate feature rt only.
Expand description

Spawns a !Send future on the local task set.

The spawned future will be run on the same thread that called spawn_local. This may only be called from the context of a local task set.


  • This function panics if called outside of a local task set.


use std::rc::Rc;
use tokio::task;

async fn main() {
    let unsend_data = Rc::new("my unsend data...");

    let local = task::LocalSet::new();

    // Run the local task set.
    local.run_until(async move {
        let unsend_data = unsend_data.clone();
        task::spawn_local(async move {
            println!("{}", unsend_data);
            // ...