pub fn block_on<'a, T: 'a>(future: impl Future<Output = T> + 'a) -> TExpand description
Runs a future to completion on the calling thread’s message loop.
Runs the provided future on the current thread, blocking until it completes.
Any tasks spawned from the same thread using spawn_local() also run concurrently.
Note that all spawned tasks are suspended after block_on() returns.
Calling block_on() again resumes the spawned tasks.
§Panics
Panics if the message loop quits before the future completes.
This can happen 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}