Colin on Nostr: Be careful about how you spawn threads in #rust This tokio::spawn(foo()); and this ...
Be careful about how you spawn threads in #rust
This
tokio::spawn(foo());
and this
tokio::spawn(async { foo().await });
Behave the same way.
However!
This does not compile:
tokio::spawn(foo().await);
and this silently does nothing.
tokio::spawn(async { foo() });
In the last example, foo is dropped entirely and never ran. So if you intend to run an async function (a Future) inside a manually specified async block, make sure to await it!
This
tokio::spawn(foo());
and this
tokio::spawn(async { foo().await });
Behave the same way.
However!
This does not compile:
tokio::spawn(foo().await);
and this silently does nothing.
tokio::spawn(async { foo() });
In the last example, foo is dropped entirely and never ran. So if you intend to run an async function (a Future) inside a manually specified async block, make sure to await it!