started ch17.3, completed half
Some checks failed
Test Gitea Actions / first (push) Successful in 18s
Test Gitea Actions / check-code (push) Failing after 12s
Test Gitea Actions / test (push) Has been skipped
Test Gitea Actions / documentation-check (push) Has been skipped

This commit is contained in:
2025-03-24 16:01:40 -06:00
parent 9ac0d0563c
commit c73d875808
3 changed files with 432 additions and 15 deletions

View File

@ -4,23 +4,23 @@ The key parts of asynchronous programming in Rust are _futures_ and Rust's `asyn
A _future_ or a promise is a value that may not be ready now but will become ready at some point in the future
In other lnaguages the same concept shows up under other names such as _task_ or _promise_.
In other languages the same concept shows up under other names such as _task_ or _promise_.
Rust provides a `Future` trait as a building block so that different async operations can be implemented with different data structures but with a common inferface.
Rust provides a `Future` trait as a building block so that different async operations can be implemented with different data structures but with a common interface.
Rust futures are types that implement the `Future` trait.
Each future holds its own information about the progress tha has been made and what "ready" means.
Each future holds its own information about the progress that has been made and what "ready" means.
You can apply the `async` keyword to blocks and functions to specify that they can be interrupted and resumed.
Within an async block or async funcion, you can use the `await` keyword to _await a future_ (that is, wait for it to become ready).
Within an async block or async function, you can use the `await` keyword to _await a future_ (that is, wait for it to become ready).
Any point where you await a future within an async or function is a potential spot for that async block or function to pause and resume.
The process of checking with a future to see if its value is avaialable yet is called _polling_.
The process of checking with a future to see if its value is available yet is called _polling_.
Some other languages, uch as C# and JavaScript, that also use `async` and `await` keyowrds for async programming.
Some other languages, such as C# and JavaScript, that also use `async` and `await` keywords for async programming.
There are some significant differences in how Rust does things, including how it handles the syntax.
@ -30,13 +30,13 @@ Rust compiles them into equivalent code using the `Future` trait, much as it com
Due to Rust providing the `Future` trait, this means that you can also implement it for your own data types you need to.
Many of the functions that we will see have return types with their own implmentations of `Future`.
Many of the functions that we will see have return types with their own implementations of `Future`.
We will return to the definition of the trait at the end of the chapeter and dig into more of how it works.
We will return to the definition of the trait at the end of the chapter and dig into more of how it works.
This may all feel a bit abstract so we will go into our first program: a little web scraper.
We will pass in two urls form the command line, fetch both of them concurrently and reutrn the result of whichever one fiinihes first.
We will pass in two URLs form the command line, fetch both of them concurrently and return the result of whichever one finishes first.
This will have a fair bit of new syntax.
@ -54,9 +54,9 @@ Here we the `tokio` crate under the hood for `trpl` becuase it is well tested an
In some cases `trpl` also renames or wraps the original APIs to keep us focused on the details relevant to this chapter.
If you want to undestand in depth of what the crate does, check out [its source code](https://github.com/rust-lang/book/tree/main/packages/trpl).
If you want to understand in depth of what the crate does, check out [its source code](https://github.com/rust-lang/book/tree/main/packages/trpl).
You will then be albe to see what crate each re-export comes from, and we have left extensive comments explaining what the crate does.
You will then be able to see what crate each re-export comes from, and we have left extensive comments explaining what the crate does.
First we will start by building a little command line tools that fetches two we pages, pulls the `<title>` element from each and print out the title of whichever page finishes that whole process first.
@ -150,7 +150,7 @@ Befor we add some code in `main` to call it. We will dive even deep into what we
When Rust sees a block mared with the `async` keyword, it compiles it into a unique, anonymous data tpye that implements the `Future` trait.
When Rust sees a function marked with `async`, it compiles it nto a non-async function whose body is an async block.
When Rust sees a function marked with `async`, it compiles it not a non-async function whose body is an async block.
An async function's return type is the type of the anonymous data type the compiler creates for that async block.