Introduction to coroutines and generators

We looked into the Tokio ecosystem earlier. We saw how it is very common to chain futures in Tokio, yielding a larger task that can then be scheduled as necessary. In practice, the following often looks like the pseudocode:

fn download_parse(url: &str) {    let task = download(url)               .and_then(|data| parse_html(data))               .and_then(|link| process_link(link))               .map_err(|e| OurErr(e));    Core.run(task);}

Here, our function takes in a URL and recursively downloads raw HTML. It then parses and collects links in the document. Our task is run in an event loop. Arguably, the control flow here is harder to follow, due to all the callbacks and how they interact. This becomes more complex with larger tasks and more conditional ...

Get Network Programming with Rust now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.