Skip to content

Commit 177aec4

Browse files
authored
Merge pull request #234 from nrc/guide-async
async/await chapter
2 parents d9a44f3 + 9c0841c commit 177aec4

File tree

9 files changed

+291
-0
lines changed

9 files changed

+291
-0
lines changed

examples/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
[workspace]
22
members = [
33
"hello-world",
4+
"hello-world-sleep",
5+
"hello-world-spawn",
6+
"hello-world-join",
47
"01_02_why_async",
58
"01_04_async_await_primer",
69
"02_02_future_trait",

examples/hello-world-join/Cargo.toml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[package]
2+
name = "hello-world-join"
3+
version = "0.1.0"
4+
authors = ["Nicholas Cameron <[email protected]>"]
5+
edition = "2021"
6+
7+
[dependencies]
8+
tokio = { version = "1.40.0", features = ["full"] }

examples/hello-world-join/src/main.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
use tokio::{spawn, time::{sleep, Duration}};
2+
3+
async fn say_hello() {
4+
// Wait for a while before printing to make it a more interesting race.
5+
sleep(Duration::from_millis(100)).await;
6+
println!("hello");
7+
}
8+
9+
async fn say_world() {
10+
sleep(Duration::from_millis(100)).await;
11+
println!("world");
12+
}
13+
14+
#[tokio::main]
15+
async fn main() {
16+
let handle1 = spawn(say_hello());
17+
let handle2 = spawn(say_world());
18+
19+
let _ = handle1.await;
20+
let _ = handle2.await;
21+
22+
println!("!");
23+
}

examples/hello-world-sleep/Cargo.toml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[package]
2+
name = "hello-world-sleep"
3+
version = "0.1.0"
4+
authors = ["Nicholas Cameron <[email protected]>"]
5+
edition = "2021"
6+
7+
[dependencies]
8+
tokio = { version = "1.40.0", features = ["full"] }
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
use std::io::{stdout, Write};
2+
use tokio::time::{sleep, Duration};
3+
4+
async fn say_hello() {
5+
print!("hello, ");
6+
// Flush stdout so we see the effect of the above `print` immediately.
7+
stdout().flush().unwrap();
8+
}
9+
10+
async fn say_world() {
11+
println!("world!");
12+
}
13+
14+
#[tokio::main]
15+
async fn main() {
16+
say_hello().await;
17+
// An async sleep function, puts the current task to sleep for 1s.
18+
sleep(Duration::from_millis(1000)).await;
19+
say_world().await;
20+
}

examples/hello-world-spawn/Cargo.toml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[package]
2+
name = "hello-world-spawn"
3+
version = "0.1.0"
4+
authors = ["Nicholas Cameron <[email protected]>"]
5+
edition = "2021"
6+
7+
[dependencies]
8+
tokio = { version = "1.40.0", features = ["full"] }
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
use tokio::{spawn, time::{sleep, Duration}};
2+
3+
async fn say_hello() {
4+
// Wait for a while before printing to make it a more interesting race.
5+
sleep(Duration::from_millis(100)).await;
6+
println!("hello");
7+
}
8+
9+
async fn say_world() {
10+
sleep(Duration::from_millis(100)).await;
11+
println!("world!");
12+
}
13+
14+
#[tokio::main]
15+
async fn main() {
16+
spawn(say_hello());
17+
spawn(say_world());
18+
// Wait for a while to give the tasks time to run.
19+
sleep(Duration::from_millis(1000)).await;
20+
}

src/SUMMARY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
- [Introduction](part-guide/intro.md)
1313
- [Concurrent programming](part-guide/concurrency.md)
14+
- [Async and Await](part-guide/async-await.md)
1415

1516
# Part 2: reference
1617

src/part-guide/async-await.md

Lines changed: 200 additions & 0 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)