Skip to content

Instantly share code, notes, and snippets.

@trickster
Last active February 9, 2022 02:11
Show Gist options
  • Save trickster/5a126cebd71498b683ecf5491b9ed2a3 to your computer and use it in GitHub Desktop.
Save trickster/5a126cebd71498b683ecf5491b9ed2a3 to your computer and use it in GitHub Desktop.
Using both Tokio and async-std together

Option 1 using async-compat

// async-compat = "0.2.1"
// async-std = {version = "1.10.0"}
// tokio = { version = "1", features = ["full"] }
use async_compat::Compat;

async fn say_hello() {
    println!("Hello, world!");
}

fn main() {
    async_std::task::block_on(Compat::new(async {
        tokio::spawn(async {
            say_hello().await; // some async function
            println!("this is also hello")
        });
    }));
}

Option 2 using async-std's tokio1 runtime

// async-std = {version = "1.10.0", features = ["attributes", "tokio1"]}
// tokio = { version = "1", features = ["full"] }

async fn say_hello() {
    println!("Hello, world!");
}

#[async_std::main]
async fn main() {
    let t = tokio::spawn(async {
        say_hello().await; // some async function
        println!("this is also hello")
    });
    t.await.unwrap()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment