Skip to content
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

async/await: various edits #249

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

Conversation

Be-ing
Copy link
Contributor

@Be-ing Be-ing commented Feb 21, 2025

Mostly minor edits. I did rewrite one particularly long and wordy paragraph.

Mostly minor edits. I did rewrite one particularly long and wordy
paragraph.
Copy link
Member

@nrc nrc left a comment

Choose a reason for hiding this comment

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

Thanks!

@@ -29,7 +29,7 @@ To get up and running as quickly as possible, you need just a little boilerplate
tokio = { version = "1", features = ["full"] }
```

And you'll use the `tokio::main` annotation on your `main` function so that it can be an async function (which is otherwise not permitted in Rust):
And you'll annotate your `main` with the `tokio::main` macro so that it can be an async function (which is otherwise not permitted in Rust):
Copy link
Member

Choose a reason for hiding this comment

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

I think the new formulation would be better with 'function' after main.

@@ -38,7 +38,7 @@ async fn main() { ... }

That's it! You're ready to write some asynchronous code!

The `#[tokio::main]` annotation initializes the Tokio runtime and starts an async task for running the code in `main`. Later in this guide we'll explain in more detail what that annotation is doing and how to use async code without it (which will give you more flexibility).
The `#[tokio::main]` macro initializes the Tokio runtime and starts an async task for running the code in `main`. Later in this guide we'll explain in more detail what that annotation is doing and how to use async code without it (which will give you more flexibility).
Copy link
Member

Choose a reason for hiding this comment

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

I prefer annotation - it describes what it is/how it works rather than how it is implemented (and avoids potential confusion with the more common ! macro usage


From here on in, I'm going to try to be precise about the terminology around tasks. When I use just 'task' I mean the abstract concept of a sequence of computation that may occur concurrently with other tasks. I'll use 'async task' to mean exactly the same thing, but in contrast to a task which is implemented as an OS thread. I'll use 'runtime's task' to mean whatever kind of task a runtime imagines, and 'tokio task' (or some other specific runtime) to mean Tokio's idea of a task.
From here on in, we'll be more precise about the terminology around tasks. A "task", unqualified, refers to the abstract concept of a sequence of computation that may occur concurrently with other tasks. An "async task" means exactly the same thing, but in contrast to a task which is implemented as an OS thread. A "runtime's task" means whatever kind of task a runtime imagines, and "tokio task" (or some other specific runtime) means Tokio's idea of a task.
Copy link
Member

Choose a reason for hiding this comment

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

I think single quotes are more appropriate since I'm not quoting anyone, but highlighting a part of speech (perhaps that an British vs American English distinction?). I kind of like using the future tense and first person perspective (though happy to change I to we) because this is setting conventions for the book rather than claiming them as universal conventions.


An async task in Rust is just a future (usually a 'big' future made by combining many others). In other words, a task is a future which is executed. However, there are times when a future is 'executed' without being a runtime's task. This kind of a future is intuitively a *task* but not a *runtime's task*. I'll spell this out more when we get to an example of it.
An async task in Rust is just a future (usually a big future made by combining many others). In other words, a task is a future which is executed. However, there are times when a future is executed without being a runtime's task. This kind of a future is intuitively a *task* but not a *runtime's task*. We'll look at an example of this in more detail later.
Copy link
Member

Choose a reason for hiding this comment

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

I think quoting 'big' here makes sense since I'm using big colloquially or even metaphorically? Not sure


`await` can only be used inside an async context, for now that means inside an async function (we'll see more kinds of async contexts later). To understand why, remember that `await` might hand over control to the runtime so that another task can execute. There is only a runtime to hand control to in an async context. For now, you can imagine the runtime like a global variable which is only accessible in async functions, we'll explain later how it really works.
`await` can only be used inside an async context, for now that means inside an async function (we'll see more kinds of async contexts later). To understand why, remember that `await` might hand over control to the runtime so that another task can execute. There is only a runtime to hand control to in an async context. For now, you can imagine the runtime like a global variable which is only accessible in async functions. We'll explain later how runtimes really work.
Copy link
Member

Choose a reason for hiding this comment

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

The 'it' here is referring to how the runtime is integrated with async and await, not the runtime itself.

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