liveBook · Manning

Asynchronous programming

As explained in Code Like a Pro in Rust

Asynchronous programming structures computation so operations can progress independently without requiring the initiating flow to wait synchronously.

Asynchronous programming lets a task start an operation that involves waiting, then makes room for other work until it completes. In Rust, async/await supports this style of control flow. It is especially useful when an application has many I/O operations in flight. section 8.2 section 1.2.2

Keep these distinctions in mind:

  • Concurrency interleaves tasks; it does not require tasks to execute simultaneously. Parallelism means simultaneous execution. section 8.0
  • Nonblocking I/O can use waiting time for other tasks instead of processing every request sequentially. Lightweight async tasks can support large numbers of concurrent operations. section 8.2
  • Async does not remove every cost. Compare the time spent waiting with the time spent processing results before expecting a performance gain. section 8.2
Figure 8.3 Comparing synchronous to asynchronous I/O
CH08_F03_Matthews

Compare blocking, sequential I/O with nonblocking I/O that leaves waiting intervals available for other work. section 8.2

Choose async for the workload

An HTTP service or a client making thousands of concurrent requests is a strong candidate. A small CLI that reads one file, or a client making a few sequential requests, is usually simpler as synchronous code. Decide early: adding async later is harder than planning for it up front. section 8.8

For a concrete Rust application, an async HTTP REST service can pair request handling with database-backed create, read, update, delete, and list operations. Account for futures, an async runtime, testing, and tracing when planning the implementation; using async also introduces complexity that sequential work may not need. section 9.0 section 8.0 section 8.8

How Code Like a Pro in Rust covers it

How deep it goes
Explains interleaving, nonblocking I/O, task overhead and selection trade-offs; identifies runtimes and futures without detailing their internals here.
Examples
Contrasts network services and high-volume request clients with sequential CLI tools, and uses a database-backed REST API as an application.
What it assumes
Assumes Rust familiarity rather than teaching basic syntax; experience with C, C++ or Java is helpful.
What you can do after
Build it from scratch: an async Rust REST API with database-backed CRUD operations.
How it connects
Connects concurrency and parallelism with nonblocking I/O, futures, async runtimes, testing and tracing.

Also explained in

Compare how 28 books explain asynchronous programming →

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.