pub fn spawn<F>(
future: F,
) -> Task<Result<<F as Future>::Output, Box<dyn Any + Send>>> ⓘwhere
F: Future + 'static,
Available on crate feature
runtime
only.Expand description
Spawns a new asynchronous task, returning a Task
for it.
Spawning a task enables the task to execute concurrently to other tasks. There is no guarantee that a spawned task will execute to completion.
let task = compio_runtime::spawn(async {
println!("Hello from a spawned task!");
42
});
assert_eq!(
task.await.unwrap_or_else(|e| std::panic::resume_unwind(e)),
42
);
§Panics
This method doesn’t create runtime. It tries to obtain the current runtime
by Runtime::with_current
.