- We have function called
do_something
that fakes real work by sleeping a bit. It is marked as an async function.
- In the
main
function we setup the Runtime.
- Calling
do_something()
does not actually execute it. It returns a Future.
async fn do_something() {
println!("Start to do something");
tokio::time::sleep(tokio::time::Duration::from_secs(1)).await;
println!("End to do something");
}
fn main() {
println!("Start");
let rt = tokio::runtime::Runtime::new().unwrap();
let future = do_something();
println!("Before block");
rt.block_on(future);
println!("End");
}
[package]
name = "demo"
version = "0.1.0"
edition = "2024"
[dependencies]
tokio = { version = "1.39.2", features = ["full"] }
Start
Before block
Start to do something
End to do something
End