O'Reilly logo

Network Programming with Rust by Abhishek Chanda

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required