File tree Expand file tree Collapse file tree 9 files changed +291
-0
lines changed Expand file tree Collapse file tree 9 files changed +291
-0
lines changed Original file line number Diff line number Diff line change 11[workspace ]
22members = [
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" ,
Original file line number Diff line number Diff line change 1+ [package ]
2+ name = " hello-world-join"
3+ version = " 0.1.0"
4+ authors = [" Nicholas Cameron <nrc@ncameron.org>" ]
5+ edition = " 2021"
6+
7+ [dependencies ]
8+ tokio = { version = " 1.40.0" , features = [" full" ] }
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 1+ [package ]
2+ name = " hello-world-sleep"
3+ version = " 0.1.0"
4+ authors = [" Nicholas Cameron <nrc@ncameron.org>" ]
5+ edition = " 2021"
6+
7+ [dependencies ]
8+ tokio = { version = " 1.40.0" , features = [" full" ] }
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 1+ [package ]
2+ name = " hello-world-spawn"
3+ version = " 0.1.0"
4+ authors = [" Nicholas Cameron <nrc@ncameron.org>" ]
5+ edition = " 2021"
6+
7+ [dependencies ]
8+ tokio = { version = " 1.40.0" , features = [" full" ] }
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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
Load Diff Large diffs are not rendered by default.
You can’t perform that action at this time.
0 commit comments