This document is intended to let readers start working with Rust's
Future quickly. Some other
useful reading includes:
- The official Tokio documentation
- Zero-cost futures in Rust
- Tokio Internals: Understanding Rust's asynchronous I/O framework from the bottom up
Future trait from
futures represents an asynchronous operation that
can fail or succeed, producing a value either way. It is like an async version of
Result. This document assumes that the reader is familiar with
Result, which is
covered in the second edition of The Rust Programming Language.
One of the most common questions about
Future seems to be, "how do I get the value out of it?"
The easiest way to do this is to call the
wait method. This runs the
Future in the current
thread, blocking all other work until it is finished.
This is not frequently the best way to run a
Future, because no other work can happen
Future completes, which completely defeats the point of using asynchronous
programming in the first place. However, it can be useful in unit tests, when debugging, or at
the top level of a simple application.
See the section on reactors for better ways to run a
Future can be modified using many functions analogous to those of
Result, such as
Futures can be combined using
Future also has a lot of functions that have no analog in
Result. Because we're talking
about aynchronous programming, now we have to choose whether we want to run two independent
operations one after the other (in sequence), or at the same time (in parallel).
For example, to get the results of two independent
Futures, we could use
and_then to run
them in sequence. However, that strategy is silly, because we are only making progress on one
Future at a time. Why not run both at the same time?
Future::join creates a new
Future that contains the results of two other
Importantly, both of the input
Futures can make progress at the same time. The new
completes only when both input
Futures complete. There's also
joining larger numbers of
join completes when both
Futures are complete,
select returns whichever
Futures completes first. This is useful for implementing timeouts, among other things.
select2 is like
select except that the two
Futures can have different value types.
Many libraries return
Futures for asynchronous operations such as network calls. Sometimes you
may want to create your own
Future. Implementing a
Future from scratch is difficult, but
there are other ways to create futures.
You can easily create a
Future from a value that is already available using the
There are similiar
Futures and types
Futures tends to produce complex types. For example, the full type of the
expression below is actually:
That is, for every transformation, we add another layer to the type of our
Future! This can
sometimes be confusing. In particular, it can be challenging to identify ways to write out the
types that aren't brittle or verbose.
In order to help the Rust compiler do type inference, below we have specify the type of
expected. It's much terser than writing the full type out, and adding another operation won't
Alternatively, we can make use of
_ to let the Rust compiler infer types for us.
Rust requires that all types in function signatures are specified.
One way to achieve this for functions that return
Futures is to specify the full return
type in the function signature. However, specifying the exact type can be verbose, brittle, and
It would be nice to be able to define a function like this:
However, the compiler doesn't like that:
This can be solved by wrapping the return type in a
Box. One day, this will be solved in a
more elegant way with the currently unstable impl Trait functionality.
Unlike functions, closures do not require all types in their signatures to be explicitly
defined, so they don't need to be wrapped in a
A more powerful way to run Futures
Composing a bunch of
Futures into a single
Future and calling
wait on it is a simple and
easy method as long as you only need to run a single
Future at a time. However, if you only
need to run a single
Future at a time, perhaps you don't need the
futures crate in the first
futures crate promises to efficiently juggle many concurrent tasks, so let's
see how that might work.
tokio-core crate has a struct called
Core which can run multiple
Core::run runs a
Future, returning its value. Unlike
though, it allows the
Core to make progress on executing other
Future objects while
Core::run is the main event loop, and it may request that new
Futures be run by calling
Handle::spawn. Note that the
Futures run by
spawn don't get to
return a value; they exist only to perform side effects.