rust-lang / rust-lang/rust

`try bikeshed`: What should the syntax be?

Open
#154,128 19 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-grammar C-discussion F-try_blocks T-lang
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

(Sub-issue of #149488)

Obviously bikeshed shouldn't literally be the syntax 🙃

Colon

Obviously the default choice for introducing types is a colon, like in let, leading to something like ``try : anyhow::Result<_> {`

However, the core trouble here is that let x: _ = and let x = are always the same, whereas try { and try: _ { would be different, as the former is homogeneous (https://github.com/rust-lang/rfcs/pull/3721) and the latter heterogeneous. That inconsistency isn't great.

Arrow

Another option would be the closure-like try -> anyhow::Result<_> {, similar to || -> anyhow::Result<_> {.

At least to me that looks aesthetically nicer than the colon, but it has a similar problem: || { … } and || -> _ { … } are the same, so it doesn't give the "and there's a conversion of some sort" happening intuition.

As

Niko originally hypothesized this feature in the form of try as anyhow::Result<_> { … } back in https://github.com/rust-lang/rust/issues/70941#issuecomment-612167041

That's nice because it's already the case that x and x as _ are different -- the latter introduces a conversion of sorts, and as _ as _ never type-checks -- which gives a similar intuition to how try { … } and try as _ { … } would be different. Plus try as _ { try as _ { … }? } also doesn't compile for the same reason that as _ as _ doesn't compile.

Other words

Because try is the keyword and the other form is try {, anything that's not { would work -- it doesn't need to be a keyword. (This is why bikeshed is working on nightly.) So we're not limited to existing reserved words (like in or whatever) if you have a good idea.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the parent issue #149488, then compare the colon, arrow, as, and other syntax proposals described here, including RFC #3721 and the earlier discussion in #70941. Done means the project has chosen and documented a syntax for distinguishing homogeneous and heterogeneous try blocks, with the relevant examples and parsing or type-checking implications resolved.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Quiet
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.