Last active
July 26, 2023 04:57
-
-
Save trvswgnr/65711bef9c5c46c0a6b57efcb14e931c to your computer and use it in GitHub Desktop.
async callback fn rust
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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