6 ms·
Replying to myself, I found this bit in the docs explaining how Tokio decorates the main: > An async fn is used as we want to enter an asynchronous context. Ho
by pfraze 6y ago
Replying to myself, I found this bit in the docs explaining how Tokio decorates the main:
> An async fn is used as we want to enter an asynchronous context. However, asynchronous functions must be executed by a runtime. The runtime contains the asynchronous task scheduler, provides evented I/O, timers, etc.
- steveklabnik 6y agoTo provide some more color on why this isn't built in, different runtimes provide different kinds of guarantees and performance profiles. A webapp has very different requirements than an embedded system, and so we don't want to provide a single runtime. The language contains the basic things needed for the ecosystem to exist, and interoperation points, and then leaves the rest to said ecosystem. (Some of those interoperation points are still being worked out, so it's not perfect yet.)
- pfraze 6y agoAs always, very elegant. Thanks for the added info.
- steveklabnik 6y agoYou're welcome, thanks :) If you want to dig deeper: * https://www.infoq.com/presentations/rust-2019/ https://www.infoq.com/presentations/rust-2019/ * https://www.infoq.com/presentations/rust-async-await/ https://www.infoq.com/presentations/rust-async-await/
- olalonde 6y agoWhen learning Rust a few months ago, I built a small client library for a REST API using reqwest (which uses Tokio). I then started writing a web app using Tide (https://github.com/http-rs/tide https://github.com/http-rs/tide). I eventually realized that it would be difficult to use the library I had built earlier since Tide uses the async-std runtime rather than Tokio. That was very disappointing. Is there any plan to make it easier to write "runtime agnostic" libraries in the future?
- steveklabnik 6y agoYes, that is what I alluded to at the end. There's a few points here that still need some interop work. The intention is to fix that, but it's non-trivial. We'll get there.
- bogeholm 6y agoSteve, just want to say thanks for your dedication - I find you’re always around when there’s a Rust thread on HN
- steveklabnik 6y agoYou’re welcome!
- mesh 6y agoI had the exact same experience. I think it can be a pretty big barrier to getting started, as you really have to lock into a sub-set of the ecosystem (i.e. I can only use crates that have support for Tokio). I understand the reasoning for not wanting this in the core language, but perhaps there could be some standard implementations which would still allow for custom runtimes.
- jkelleyrtp 6y agoYou can make async-std mimic a tokio runtime by adding “tokio02” or “tokio03” to the list of features for async_std. At its core, futures and future combinators work under all runtimes, it’s just some features will complain if they don’t detect the tokio runtime.
- le_didil 6y agothat's a good point, when playing a bit with rust last year I found that the libraries that deal with "async stuff" (like http clients, db clients etc) are mostly split, some use tokio and some use async-std, and they were incompatible. Not sure if the situation has improved now but it looked like an ecosystem split at the time.
- 1f60c 6y agoI knew you needed to use a library like Tokio with asynchronous Rust, but I never understood why, so thanks. That said, the last time I looked at it, Tokio was much too complex for my tastes.