pub fn block_on<'a, T: 'a>(future: impl Future<Output = T> + 'a) -> T
Expand description
Runs a future to completion on the calling threads message loop.
This runs the provided future on the current thread, blocking until it is
complete. Also runs any tasks [spawn
]ed from the same thread. Note that
any spawned tasks will be suspended after block_on
returns. Calling
block_on
again will resume previously spawned tasks.
ยงPanics
Panics when quitting out of the message loop without the future being
ready. This can happen when calling when the future or any spawned task
calls the PostQuitMessage()
winapi function.
Examples found in repository?
examples/basic.rs (lines 21-34)
19fn main() {
20 println!("hello");
21 block_on(async {
22 let task = spawn_local(async {
23 println!("async hello 1");
24 poll_n_times(3).await;
25 println!("async bye 1");
26 "async 1 result"
27 });
28
29 println!("async hello 2");
30 poll_n_times(2).await;
31 println!("async bye 2");
32
33 println!("{}", task.await);
34 });
35 println!("bye");
36}
More examples
examples/threads.rs (lines 22-26)
19fn main() {
20 thread::spawn(|| {
21 println!("thread hello");
22 block_on(async {
23 println!("thread async hello");
24 poll_n_times(3).await;
25 println!("thread async bye");
26 });
27 println!("thread bye");
28 });
29
30 println!("main hello");
31 block_on(async {
32 println!("main async hello");
33 poll_n_times(3).await;
34 println!("main async bye");
35 });
36 println!("main bye");
37}