Introduction

Futures provide the building blocks for asynchronous computations with zero-cost abstraction. Asynchronous communication is useful for handling timeouts, computing across thread pools, network responses, and any function that does not immediately return a value.

In a synchronous block, the computer would execute each command sequentially after waiting for each command to return a value. If you were to apply the synchronous model when sending an email, you would send the message, stare at your inbox, and wait until you have received a response from your recipient.

Fortunately, life does not work synchronously. After we send an email, we could switch to another application or get off our chair. We can start performing other tasks such as getting the groceries, cooking dinner, or reading a book. Our attention can focus on, and perform, other tasks simultaneously. Periodically, we will check our inbox for a response from our recipient. The process of periodically checking for the new message illustrates the asynchronous model. Unlike humans, computers can check for a new message in our inbox and perform other tasks at the same time.

Rust’s futures work by implementing the polling model, which utilizes a central component (for example, a piece of software, hardware devices, and network hosts) to handle status reports from other components. The central, or master, component sends signals to other components repetitively until the master component receives an update, an interruption signal, or the polling event has timed out.

To get a better understanding on how concurrency works within Rust's model, you can view Alex Crichton's concurrency presentations at https://github.com/alexcrichton/talks. Throughout our recipes, we will be using the futures::executor::block_on function within our main thread to return values. This is intentionally done for demonstrative purposes only. In a real application, you would use block_on within another a separate thread and your functions would return some sort of futures::Future implementation such as futures::future::FutureResult.
At the time of writing, futures is performing a lot of developmental changes throughout its code base. You can view futures' RFCs (Request For Comments) on their official repository at https://github.com/rust-lang-nursery/futures-rfcs
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.138.37.20