简体   繁体   中英

Why do std::task, core::task, and tokio::task all exist?

Rust seems to have 3 different types of tasks,

Why do these three tasks exist?

Those are modules, so the fact that they all coexist and have the same name doesn't really imply anything. Any arbitrary crate can create a task module (or type or trait or...). That's why most programming languages have namespaces to start with — so we can have name collisions

  1. std::task is core::task , re-exported under a different name. This contains the building blocks for creating futures themselves and the executors that drive them. A very small handful of people will need to use these types.

  2. tokio::task allows creating Tokio tasks : "asynchronous green-threads". These are the semantic equivalent of threads for an asynchronous world. See the Tokio website's section on spawning tasks for more detail.

    async_std::task is the same thing but for a different executor. async-std tasks are distinct from Tokio tasks and are not interchangeable.

  3. futures::task is kind of a mish-mash between the standard library's module and those of the executors. This is because of its history — the futures crate was the implementation of futures before they were moved into the standard library. Now it contains a re-export of the standard library's types plus some further tools for creating an executor as well as traits for spawning tasks on the executor provided by the futures library.

See also:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM