Overview

Futures, hinted at earlier in the guide, are the building block used to manageasynchronous logic. They are the underlying asynchronous abstraction used byTokio.

A future is a value that represents the completion of an asynchronouscomputation. Usually, the future completes due to an event that happenselsewhere in the system. While we’ve been looking at things from the perspectiveof basic I/O, you can use a future to represent a wide range of events, e.g.:

  • A database query, when the query finishes, the future is completed, andits value is the result of the query.

  • An RPC invocation to a server. When the server replies, the future iscompleted, and its value is the server’s response.

  • A timeout. When time is up, the future is completed, and its value is().

  • A long-running CPU-intensive task, running on a thread pool. When the taskfinishes, the future is completed, and its value is the return value of thetask.

  • Reading bytes from a socket. When the bytes are ready, the future iscompleted – and depending on the buffering strategy, the bytes might bereturned directly, or written as a side-effect into some existing buffer.

Applications built with Tokio are structured in terms of futures. Tokio takesthese futures and drives them to completion.

Next up: Implementing futures