Async concepts using async-std
Rust Futures have the reputation of being hard. We don't think this is the case. They are, in our opinion, one of the easiest concurrency concepts around and have an intuitive explanation.
[Rust Futures]的名聲說它很難蹈垢。我們認(rèn)為情況并非如此挖炬。在我們看來,它們是最簡單的并發(fā)概念之一烹吵,并且有一個(gè)直觀的解釋。
However, there are good reasons for that perception. Futures have three concepts at their base that seem to be a constant source of confusion: deferred computation, asynchronicity and independence of execution strategy.
然而腹忽,這種看法有充分的理由猖败。Futures 有三個(gè)基本概念,似乎是經(jīng)常引起混亂:延遲計(jì)算烹俗,異步性和執(zhí)行策略的獨(dú)立性爆侣。
These concepts are not hard, but something many people are not used to. This base confusion is amplified by many implementations oriented on details. Most explanations of these implementations also target advanced users, and can be hard for beginners. We try to provide both easy-to-understand primitives and approachable overviews of the concepts.
這些概念并不難,但很多人并不習(xí)慣幢妄。許多面向細(xì)節(jié)的實(shí)現(xiàn)放大了這種基本混淆兔仰。對這些實(shí)現(xiàn)的大多數(shù)解釋也針對高級用戶,對初學(xué)者來說可能很難蕉鸳。我們試圖提供易于理解的原語和概念的可接近的概述乎赴。
Futures are a concept that abstracts over how code is run. By themselves, they do nothing. This is a weird concept in an imperative language, where usually one thing happens after the other - right now.
Futures 是一個(gè)概念,它抽象了代碼是如何運(yùn)行的潮尝。他們一個(gè)人什么也不做榕吼。在命令式語言中,這是一個(gè)奇怪的概念勉失,通常一件事發(fā)生在另一件事之后——現(xiàn)在羹蚣。
So how do Futures run? You decide! Futures do nothing without the piece of code executing them. This part is called an executor. An executor decides when and how to execute your futures. The async-std::task
module provides you with an interface to such an executor.
那么,F(xiàn)utures 如何運(yùn)行戴质?你決定度宦!如果沒有這段代碼的執(zhí)行踢匣,F(xiàn)utures 就啥都不干。這部分被稱為“executor”戈抄。executor決定何時(shí)以及如何執(zhí)行你的futures离唬。“async_std::task”模塊為您提供了這樣一個(gè)executor 的接口划鸽。
Let's start with a little bit of motivation, though.
不過输莺,讓我們先從一點(diǎn)動力開始。