Skip to content

Instantly share code, notes, and snippets.

@trvswgnr
Last active July 26, 2023 04:57
Show Gist options
  • Save trvswgnr/65711bef9c5c46c0a6b57efcb14e931c to your computer and use it in GitHub Desktop.
Save trvswgnr/65711bef9c5c46c0a6b57efcb14e931c to your computer and use it in GitHub Desktop.
async callback fn rust
use futures::Future;
async fn async_fn_with_async_cb<T: Future>(f: fn(String) -> T) -> T::Output {
// we do some shit here and then end up with a string
let x = "`this is the result of something`".to_string();
f(x).await
}
#[tokio::main]
async fn main() {
let f = async_fn_with_async_cb(|x: String| async move {
println!("got {} from func", x);
// just simulating some async things
println!("waiting 1 sec...");
tokio::time::sleep(std::time::Duration::from_secs(1)).await;
// then we can optionally return something
"cool beans".to_string()
});
let result = f.await;
println!("{result}");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment