Skip to content

Add exercise async1#2382

Open
senekor wants to merge 4 commits intorust-lang:mainfrom
senekor:senekor/rvsyvlvuzyvu
Open

Add exercise async1#2382
senekor wants to merge 4 commits intorust-lang:mainfrom
senekor:senekor/rvsyvlvuzyvu

Conversation

@senekor
Copy link
Copy Markdown
Contributor

@senekor senekor commented Apr 18, 2026

The goal here was to get the first bit of "muscle memory" for using the async and await keywords. The little story should make it more intuitive for users why asynchronous programming is needed in the first place.

This exercise can be moved to the location corresponding to the book in a later PR, to keep the diff of this one clean.

@senekor senekor force-pushed the senekor/rvsyvlvuzyvu branch 2 times, most recently from b2f334e to 7f50737 Compare April 18, 2026 05:06
@senekor senekor force-pushed the senekor/rvsyvlvuzyvu branch from 7f50737 to 46533ad Compare April 18, 2026 05:20
The goal here was to get the first bit of "muscle memory" for using the
async and await keywords. The little story should make it more intuitive
for users why asynchronous programming is needed in the first place.

This exercise will be moved to the location corresponding to the book in
a later commit, to keep the diff of this one clean.
@senekor senekor force-pushed the senekor/rvsyvlvuzyvu branch from 46533ad to abc8969 Compare April 18, 2026 21:31
Copy link
Copy Markdown
Contributor

@mo8it mo8it left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like

  • The requirement of adding async and .await
  • Spawning tasks, awaiting them and then checking that they are done
  • The strory about splitting tasks among different workers

I don't like

  • The usage of atomics
  • The work done in each task. Printing locks under the hood so the tasks will run mostly sequentially even if the runtime was multi-threaded
  • The story details about the boys and soccer. Some might find it a bit childish for tasks meant mainly for adults

What about letting the tasks do some calculation and return the result? Then all three results could be checked. This way, we don't need atomics or printing.

Comment thread dev/Cargo.toml Outdated
publish = false

[dependencies]
tokio = { version = "1.52.1", features = ["rt"] }
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

version = "1" should be enough

Comment thread exercises/24_async/async1.rs Outdated
Comment thread exercises/24_async/async1.rs Outdated
Comment thread exercises/24_async/async1.rs Outdated
// finish the chores earlier and have more time left to play soccer.
//
// Let's simulate this using asynchronous programming. Each boy is represented
// as an asynchronous task, which can be executed concurrently (they can be
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here, you say that they can be working at the same time. But in main, you create a single threaded runtime.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"concurrently" is the more precise word. I think it's good for teaching to use a single-threaded runtime, to show that async can make a difference even without multi-threading. Though, as you mentioned, just printing to the console might not be best for that.

Comment thread exercises/24_async/async1.rs Outdated
@mo8it
Copy link
Copy Markdown
Contributor

mo8it commented Apr 25, 2026

What about using tokio::fs to open three files asynchronously, reading their content by parsing each line as a number and summing them up?

Something like that does actual work and is a valid usage for async.

@mo8it
Copy link
Copy Markdown
Contributor

mo8it commented Apr 25, 2026

The story could be something like teachers want to calculate the mean grade for three different classes. Instead of only one teacher doing all the work or doing it sequentially, they can do it async.

senekor added 3 commits April 25, 2026 15:33
- Remove confusing use of atomics. Use return values of async tasks
  instead, to ensure all tasks are awaited.
- Remove use of `println!()`, which uses a global lock and cannot be
  executed in parallel.
@senekor
Copy link
Copy Markdown
Contributor Author

senekor commented Apr 25, 2026

Agree with most of these points. I redid the exercise to hopefully address them. I'm not sure yet about using a multi-threaded runtime and using file IO. Yes, it would be more realistic, but I'm concerned it might be more complicated and distract from the intended lesson here - building a little async/await muscle memory.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants