liveBook · Manning

Future

As explained in Code Like a Pro in Rust

Future is a Rust trait representing a computation that may complete later and eventually produce an output or remain pending.

A Future represents an asynchronous computation whose output is available later. An async function or block returns a future, not its eventual value. Futures are lazy: creating one does not run its body. Await it in an async context or spawn it on a runtime to make progress. section 8.3 section 8.4

Rust supplies the Future trait, async, and .await, but does not prescribe a runtime. Tokio supplies an executor and scheduler; #[tokio::main] starts the runtime for an async main function. Prefer async and .await over polling futures directly unless an interface requires it. section 8.1 section 8.3.1 section 8.4

pub trait Future {
   type Output;
   fn poll(self: Pin<&mut Self>, cx: &mut Context<‘_>) -> Poll<Self::Output>;
}

The trait signature exposes the implementation boundary: poll receives a pinned mutable reference and a context, then returns Poll<Self::Output>. An awaited future is polled through this interface. section 8.6 section 8.4

Await, discard, or spawn

In an async main function, `async { println!("first"); }.await` runs before the next statement. Assigning an async block to `_future` without awaiting or spawning it never runs its body. `tokio::task::spawn(async { ... })` schedules a task, but dropping its join handle and exiting does not guarantee that it finishes; await the handle when completion matters. section 8.4

Returning a future through a trait interface requires more care than writing an async function. A boxed `dyn Future` alone can fail at `.await` because it is not `Unpin`; returning `Pin<Box<dyn Future<Output = T>>>` and constructing it with `Box::pin(async { ... })` addresses that error. Borrowed inputs may also require an explicit lifetime on the returned future, while tasks that must cross threads may require `Send`. section 8.6

Keep blocking calls off the async scheduler. For synchronous work that must run inside an async workflow, `tokio::task::spawn_blocking` runs the function on Tokio-managed blocking threads and returns an awaitable future. For independent async work, `tokio::join!` or `tokio::select!` provides explicit concurrency; spawning tasks can also introduce parallelism. section 8.7 section 8.11

How Code Like a Pro in Rust covers it

How deep it goes
Explains lazy execution and polling, then works through pinned, boxed futures for an async observer; it does not implement an executor.
Examples
Contrasts awaited, discarded, and spawned async blocks; builds a pinned future-returning observer trait and tests its update.
What it assumes
Assumes familiarity with Rust functions and traits while introducing how async blocks produce futures and why returned trait-object futures need pinning.
What you can do after
Apply it via Tokio to await or spawn work, return pinned futures from trait methods, and isolate blocking calls.
How it connects
Connects futures to async and await, Tokio runtimes, task spawning, pinning, and the observer pattern.

Also explained in

Compare how 8 books explain future →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Code Like a Pro in Rust. The section links point to the parts of the book it draws on.