Add explanation about how Rust's postfix await syntax is ergonomic#262
Open
nt54hamnghi wants to merge 2 commits into
Open
Add explanation about how Rust's postfix await syntax is ergonomic#262nt54hamnghi wants to merge 2 commits into
nt54hamnghi wants to merge 2 commits into
Conversation
edd0bdb to
543ec31
Compare
nrc
reviewed
May 14, 2026
543ec31 to
7de1778
Compare
nrc
reviewed
May 19, 2026
Member
nrc
left a comment
There was a problem hiding this comment.
Just one comment on the examples, otherwise looks good, thanks!
| In Rust, the syntax for using await is `some_future.await`, i.e., it is a postfix keyword used with the `.` operator. That means it can be used ergonomically in chains of method calls and field accesses. This is in contrast to languages like Python or JavaScript, where `await` is a prefix operator placed before an expression, such as `await some_function()`. | ||
|
|
||
| Consider the following functions: | ||
| To see why postfix await is often more ergonomic, suppose you're calling an async function that makes a network request and want to access the status code of the response. With the prefix `await` syntax, you would need to prepend `await` to `fetch()`, then wrap the expression in parentheses to access the status code, like `(await fetch()).status_code`. In Rust, you can write `fetch().await.status_code?`. This becomes especially helpful in longer chains. E.g., an expression with two prefix awaits looks like `(await (await fetch()).json()).data`, whereas the Rust equivalent is `fetch().await.json().await.data?`, which reads more naturally. |
Member
There was a problem hiding this comment.
The postfix versions a have a ?, but the prefix ones don't. For the ?s to be really significant, they need to be after the await rather than the field access. For the prefix versions they could be used after the parens.
Contributor
Author
There was a problem hiding this comment.
Thanks! I'll add ? after .await for the postfix versions.
For the prefix versions, should these be treated as hypothetical Rust prefix syntax, or syntax from languages that actually use it? My initial intent was to use valid Python so the syntax is recognizable, but I'm happy to change it. Just want to make sure we're aligned.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
No description provided.