async_std/task/
spawn.rs

1use std::future::Future;
2
3use crate::task::{Builder, JoinHandle};
4
5/// Spawns a task.
6///
7/// This function is similar to [`std::thread::spawn`], except it spawns an asynchronous task.
8///
9/// [`std::thread`]: https://doc.rust-lang.org/std/thread/fn.spawn.html
10///
11/// # Examples
12///
13/// ```
14/// # async_std::task::block_on(async {
15/// #
16/// use async_std::task;
17///
18/// let handle = task::spawn(async {
19///     1 + 2
20/// });
21///
22/// assert_eq!(handle.await, 3);
23/// #
24/// # })
25/// ```
26pub fn spawn<F, T>(future: F) -> JoinHandle<T>
27where
28    F: Future<Output = T> + Send + 'static,
29    T: Send + 'static,
30{
31    Builder::new().spawn(future).expect("cannot spawn task")
32}