rmanoka / async-scoped

A scope for async_std and tokio to spawn non-static futures
120 stars 14 forks source link

Doc request: full minimal working example, including main #32

Open sullyj3 opened 4 months ago

sullyj3 commented 4 months ago

Hi! I'm interested in using this library. However, as someone who hasn't done concurrency in rust before, I'm not quite sure how to get started.

The main docs page has this example:

async fn scoped_futures() {
    let not_copy = String::from("hello world!");
    let not_copy_ref = &not_copy;
    let (foo, outputs) = async_scoped::AsyncStdScope::scope_and_block(|s| {
        for _ in 0..10 {
            let proc = || async {
                assert_eq!(not_copy_ref, "hello world!");
                eprintln!("Hello world!")
            };
            s.spawn(proc());
        }
        42
    });
    assert_eq!(foo, 42);
    assert_eq!(outputs.len(), 10);
}

But I'm not sure how to execute an async fn. This means as a beginner, I have to go and look at the async-std or tokio docs to understand how to use this library, which is perhaps intended to be an abstraction layer over the top of them.

sullyj3 commented 4 months ago

for tokio, it seems like I need:

#[tokio::main]
async fn main() {
  scoped_futures().await;
}
sullyj3 commented 4 months ago

I'm happy to PR this example if that sounds good

rmanoka commented 4 months ago

Hi, please feel free to provide a PR to add docs. Yes, we assume a bit of working knowledge of futures/asyncs & in this crate, as this crate is about handling references/life-times in async programs.