mirror of https://github.com/rust-lang/async-book
ignore running some code, fix edition for other part of code
This commit is contained in:
parent
15281c6a3d
commit
658ce12b21
|
@ -8,7 +8,7 @@ thread. In a typical threaded application, if you wanted to download two
|
|||
different webpages at the same time, you would spread the work across two
|
||||
different threads, like this:
|
||||
|
||||
```rust
|
||||
```rust,ignore
|
||||
{{#include ../../examples/01_02_why_async/src/lib.rs:get_two_sites}}
|
||||
```
|
||||
|
||||
|
@ -22,7 +22,7 @@ to eliminate. We can rewrite the function above using Rust's
|
|||
`async`/`.await` notation, which will allow us to run multiple tasks at
|
||||
once without creating multiple threads:
|
||||
|
||||
```rust
|
||||
```rust,ignore
|
||||
{{#include ../../examples/01_02_why_async/src/lib.rs:get_two_sites_async}}
|
||||
```
|
||||
|
||||
|
|
|
@ -9,14 +9,14 @@ blocked `Future`s will yield control of the thread, allowing other
|
|||
|
||||
To create an asynchronous function, you can use the `async fn` syntax:
|
||||
|
||||
```rust
|
||||
async fn do_something() { ... }
|
||||
```rust,edition2018
|
||||
async fn do_something() { /* ... */ }
|
||||
```
|
||||
|
||||
The value returned by `async fn` is a `Future`. For anything to happen,
|
||||
the `Future` needs to be run on an executor.
|
||||
|
||||
```rust
|
||||
```rust,edition2018
|
||||
{{#include ../../examples/01_04_async_await_primer/src/lib.rs:hello_world}}
|
||||
```
|
||||
|
||||
|
@ -29,16 +29,16 @@ other tasks to run if the future is currently unable to make progress.
|
|||
For example, imagine that we have three `async fn`: `learn_song`, `sing_song`,
|
||||
and `dance`:
|
||||
|
||||
```rust
|
||||
async fn learn_song() -> Song { ... }
|
||||
async fn sing_song(song: Song) { ... }
|
||||
async fn dance() { ... }
|
||||
```rust,ignore
|
||||
async fn learn_song() -> Song { /* ... */ }
|
||||
async fn sing_song(song: Song) { /* ... */ }
|
||||
async fn dance() { /* ... */ }
|
||||
```
|
||||
|
||||
One way to do learn, sing, and dance would be to block on each of these
|
||||
individually:
|
||||
|
||||
```rust
|
||||
```rust,ignore
|
||||
{{#include ../../examples/01_04_async_await_primer/src/lib.rs:block_on_each}}
|
||||
```
|
||||
|
||||
|
@ -48,7 +48,7 @@ we can sing it, but it's possible to dance at the same time as learning and
|
|||
singing the song. To do this, we can create two separate `async fn` which
|
||||
can be run concurrently:
|
||||
|
||||
```rust
|
||||
```rust,ignore
|
||||
{{#include ../../examples/01_04_async_await_primer/src/lib.rs:block_on_main}}
|
||||
```
|
||||
|
||||
|
|
|
@ -15,14 +15,14 @@ Let's add some dependencies to the `Cargo.toml` file:
|
|||
Now that we've got our dependencies out of the way, let's start writing some
|
||||
code. We have some imports to add:
|
||||
|
||||
```rust
|
||||
```rust,ignore
|
||||
{{#include ../../examples/01_05_http_server/src/lib.rs:imports}}
|
||||
```
|
||||
|
||||
Once the imports are out of the way, we can start putting together the
|
||||
boilerplate to allow us to serve requests:
|
||||
|
||||
```rust
|
||||
```rust,ignore
|
||||
{{#include ../../examples/01_05_http_server/src/lib.rs:boilerplate}}
|
||||
```
|
||||
|
||||
|
@ -35,7 +35,7 @@ You can also inspect the request itself, which contains information such as
|
|||
the request URI, HTTP version, headers, and other metadata. For example, we
|
||||
can print out the URI of the request like this:
|
||||
|
||||
```rust
|
||||
```rust,ignore
|
||||
println!("Got request at {:?}", req.uri());
|
||||
```
|
||||
|
||||
|
@ -47,14 +47,14 @@ request to another website using Hyper's HTTP client.
|
|||
|
||||
We start by parsing out the URL we want to request:
|
||||
|
||||
```rust
|
||||
```rust,ignore
|
||||
{{#include ../../examples/01_05_http_server/src/lib.rs:parse_url}}
|
||||
```
|
||||
|
||||
Then we can create a new `hyper::Client` and use it to make a `GET` request,
|
||||
returning the response to the user:
|
||||
|
||||
```rust
|
||||
```rust,ignore
|
||||
{{#include ../../examples/01_05_http_server/src/lib.rs:get_request}}
|
||||
```
|
||||
|
||||
|
|
Loading…
Reference in New Issue