Skip to content

Commit fe8224b

Browse files
authored
Merge pull request #95 from shnmorimoto/add_4_5
add 4_5
2 parents f914a54 + 2bd85dd commit fe8224b

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

chapter4/Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ path = "src/4_2_5/main.rs"
2626
name = "4_3_1"
2727
path = "src/4_3_1/main.rs"
2828

29+
[[bin]]
30+
name = "4_5"
31+
path = "src/4_5/main.rs"
32+
2933
[dependencies]
3034
lib = { path = "../lib" }
3135
tokio = { version = "1.5.0", features = ["full"] }

chapter4/src/4_5/main.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
use tokio::time::{self, Duration};
2+
3+
// tokio::time::sleepを使って、決まった時間を図るタイマーを作る
4+
#[tokio::main]
5+
async fn main() {
6+
let wait_sec = 5;
7+
let (_, mut wait) = tokio::sync::oneshot::channel::<()>();
8+
let sleep = time::sleep(Duration::from_secs(wait_sec));
9+
tokio::pin!(sleep);
10+
11+
loop {
12+
tokio::select! {
13+
_ = &mut sleep => {
14+
println!("{} secondes elapsed", wait_sec);
15+
break;
16+
}
17+
_ = &mut wait => {}
18+
}
19+
}
20+
}

0 commit comments

Comments
 (0)