Skip to content

Add explanation about how Rust's postfix await syntax is ergonomic#262

Open
nt54hamnghi wants to merge 2 commits into
rust-lang:masterfrom
nt54hamnghi:nt54hamnghi/postfix-await
Open

Add explanation about how Rust's postfix await syntax is ergonomic#262
nt54hamnghi wants to merge 2 commits into
rust-lang:masterfrom
nt54hamnghi:nt54hamnghi/postfix-await

Conversation

@nt54hamnghi
Copy link
Copy Markdown
Contributor

No description provided.

@nt54hamnghi nt54hamnghi force-pushed the nt54hamnghi/postfix-await branch from edd0bdb to 543ec31 Compare May 12, 2026 01:23
Comment thread src/part-guide/async-await.md Outdated
Comment thread src/part-guide/async-await.md Outdated
Comment thread src/part-guide/async-await.md Outdated
@nt54hamnghi nt54hamnghi force-pushed the nt54hamnghi/postfix-await branch from 543ec31 to 7de1778 Compare May 15, 2026 11:18
@nt54hamnghi nt54hamnghi requested a review from nrc May 15, 2026 11:36
Copy link
Copy Markdown
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.

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.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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.

Copy link
Copy Markdown
Contributor Author

@nt54hamnghi nt54hamnghi May 20, 2026

Choose a reason for hiding this comment

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

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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Either is fine

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