Skip to content

async/await chapter #234

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions examples/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
[workspace]
members = [
"hello-world",
"hello-world-sleep",
"hello-world-spawn",
"hello-world-join",
"01_02_why_async",
"01_04_async_await_primer",
"02_02_future_trait",
Expand Down
8 changes: 8 additions & 0 deletions examples/hello-world-join/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[package]
name = "hello-world-join"
version = "0.1.0"
authors = ["Nicholas Cameron <[email protected]>"]
edition = "2021"

[dependencies]
tokio = { version = "1.40.0", features = ["full"] }
23 changes: 23 additions & 0 deletions examples/hello-world-join/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
use tokio::{spawn, time::{sleep, Duration}};

async fn say_hello() {
// Wait for a while before printing to make it a more interesting race.
sleep(Duration::from_millis(100)).await;
println!("hello");
}

async fn say_world() {
sleep(Duration::from_millis(100)).await;
println!("world");
}

#[tokio::main]
async fn main() {
let handle1 = spawn(say_hello());
let handle2 = spawn(say_world());

let _ = handle1.await;
let _ = handle2.await;

println!("!");
}
8 changes: 8 additions & 0 deletions examples/hello-world-sleep/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[package]
name = "hello-world-sleep"
version = "0.1.0"
authors = ["Nicholas Cameron <[email protected]>"]
edition = "2021"

[dependencies]
tokio = { version = "1.40.0", features = ["full"] }
20 changes: 20 additions & 0 deletions examples/hello-world-sleep/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
use std::io::{stdout, Write};
use tokio::time::{sleep, Duration};

async fn say_hello() {
print!("hello, ");
// Flush stdout so we see the effect of the above `print` immediately.
stdout().flush().unwrap();
}

async fn say_world() {
println!("world!");
}

#[tokio::main]
async fn main() {
say_hello().await;
// An async sleep function, puts the current task to sleep for 1s.
sleep(Duration::from_millis(1000)).await;
say_world().await;
}
8 changes: 8 additions & 0 deletions examples/hello-world-spawn/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[package]
name = "hello-world-spawn"
version = "0.1.0"
authors = ["Nicholas Cameron <[email protected]>"]
edition = "2021"

[dependencies]
tokio = { version = "1.40.0", features = ["full"] }
20 changes: 20 additions & 0 deletions examples/hello-world-spawn/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
use tokio::{spawn, time::{sleep, Duration}};

async fn say_hello() {
// Wait for a while before printing to make it a more interesting race.
sleep(Duration::from_millis(100)).await;
println!("hello");
}

async fn say_world() {
sleep(Duration::from_millis(100)).await;
println!("world!");
}

#[tokio::main]
async fn main() {
spawn(say_hello());
spawn(say_world());
// Wait for a while to give the tasks time to run.
sleep(Duration::from_millis(1000)).await;
}
1 change: 1 addition & 0 deletions src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

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

# Part 2: reference

Expand Down
200 changes: 200 additions & 0 deletions src/part-guide/async-await.md

Large diffs are not rendered by default.

Loading