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

Clarify the HRTB chapter #330

Merged
merged 1 commit into from
Jan 22, 2022
Merged

Conversation

fmease
Copy link
Member

@fmease fmease commented Jan 19, 2022

Recently, someone asked for some clarifications in the easy questions thread on the unofficial Rust subreddit regarding the HRTB chapter.

They were confused by the fact that they could not simply copy&paste the desugared and pseudo Rust code – with the modification F: Fn(&'??? (u8, u16)) -> &'??? u8for<'a> F: Fn(&'a (u8, u16)) -> &'a u8 applied – into the playground and have it work out of the box.

The pseudo Rust syntax is introduced in the lifetimes chapter (basically a simplified version of -Z unpretty=hir with lifetime annotations in expressions) as indeed mentioned above the HRTB code example. However, it appears that this user overlooked this sentence and did not read the lifetimes chapter. I strongly believe that this is not an uncommon situation: Users probably find this chapter by using a search engine after facing the for<>-syntax for the first time and getting to know it's called a HRTB or by reading the Nomicon nonlinearly/out of order. They merely skim the text and focus on the code examples without realizing one of them is not actual Rust code.

To avoid future confusion, I

  • added a // NOTE to the pseudo Rust code (simply mirroring the note in the lifetimes chapter)
  • made the mention of the lifetimes chapter a hyperlink
  • added the full version of the modified code at the bottom
  • noted one can swap F and for<'a> (not as relevant, just a general clarification)

Feel free to provide improvements to my changes or to outright close this PR if you do not feel this is necessary/useful. I am aware this makes the chapter more verbose.

Mark the desugared code example as pseudo Rust code just like in the lifetimes chapter.
Add the full version of the modified code at the end.
@dcormier
Copy link

However, it appears that this user overlooked this sentence and did not read the lifetimes chapter. I strongly believe that this is not an uncommon situation: Users probably find this chapter by using a search engine after facing the for<>-syntax for the first time and getting to know it's called a HRTB or by reading the Nomicon nonlinearly/out of order.

"This user," here. Just wanted to say that this is exactly what happened. I was trying to understand the for<> syntax after stumbling across it somewhere, and whatever it was that I Googled led me to that page in the 'nomicon as the first result.

Copy link
Member

@JohnTitor JohnTitor left a comment

Choose a reason for hiding this comment

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

Great improvement, thanks!

@JohnTitor JohnTitor merged commit f4d1b42 into rust-lang:master Jan 22, 2022
@fmease fmease deleted the hrtb-clarifications branch January 23, 2022 00:10
@Gankra
Copy link
Contributor

Gankra commented Jan 24, 2022

thanks!

ehuss added a commit to ehuss/rust that referenced this pull request Feb 1, 2022
Update books

## nomicon

4 commits in 66d097d3d80e8f88c288c6879c7c2b909ecf8ad4..9493715a6280a1f74be759c7e1ef9999b5d13e6f
2022-01-05 05:45:21 +0900 to 2022-01-27 19:00:32 -0800
- send-and-sync: it's -&gt; its (rust-lang/nomicon#332)
- Clarify the HRTB chapter (rust-lang/nomicon#330)
- Clarify repr(transparent) in other-reprs (rust-lang/nomicon#329)
- Make C code more recognizably C (rust-lang/nomicon#331)

## reference

10 commits in 4dee6eb63d728ffb9e7a2ed443e9ada9275c69d2..411c2f0d5cebf48453ae2d136ad0c5e611d39aec
2022-01-18 09:26:33 -0800 to 2022-01-30 12:46:37 -0800
- paths.md: update comments of `Canoical paths` section (rust-lang/reference#1146)
- Add undocumented outer attributes above StructExpr fields (rust-lang/reference#1150)
-  (rust-lang/reference#1148)
- Fix micro typo in async/unsafe function docs (rust-lang/reference#1145)
- Note difference in CTFE timing between associated and free constants (rust-lang/reference#1120)
- Update the Preludes chapter for the 2021 edition changes to the standard library prelude (rust-lang/reference#1136)
- Link to associated constants section rather than glossary (rust-lang/reference#1141)
- functions.md: replace `argument` with `parameter` (rust-lang/reference#1142)
- Improve rendering (rust-lang/reference#1143)
- (minor) link references and replace wording by syntax definition (rust-lang/reference#1139)

## book

24 commits in f17df27fc14696912c48b8b7a7a8fa49e648088d..98904efaa4fc968db8ff59cf2744d9f7ed158166
2022-01-18 17:46:28 -0500 to 2022-01-29 21:22:31 -0500
- Snapshot of chapter 17 for nostarch
- Remove the section on object safety.
- Don't put a hyphen in 'object safe'. Fixes rust-lang/book#2960.
- Clarify that add_text on Post will work in any state. Fixes rust-lang/book#2159.
- Fix incorrect descriptions of what the code is doing. Fixes rust-lang/book#2745.
- Fix link style and inclusion in print
- Snapshot of ch16 for nostarch
- Cut discussion of threading models Rust *doesn't* support.
- Update a quote of compiler output
- Move transfers between threads, not shares. Fixes rust-lang/book#2843.
- Ch20-02 Remove reference to a long-gone "trick"
- Clarify translations a bit
- Added a mention to the translations appendix
- Fix listing number from `8-5` to `9-5` in `ch09-02`
- Moving example into blockquote means it can't be extracted to a listing project
- Move a link to the end with all the other links
- Propagate edits back to ch 9
- Responding to edits in chapter 9
- Update to 1.58
- Snapshot of chapter 15 for nostarch
- Change 'only difference' to 'main difference'. Fixes rust-lang/book#1581.
- Add a back reference to tuple struct syntax. Fixes rust-lang/book#1916
- Add a link to a section reference
- Remove an outdated example that says it won't compile but it does

## rustc-dev-guide

2 commits in 78dd6a4684cf8d6b72275fab6d0429ea40b66338..8763adb62c712df69b1d39ea3e692b6d696cc4d9
2022-01-18 14:44:26 -0300 to 2022-01-26 14:01:40 -0800
- git.md: Expanded a note to try to stress what you need to do if you're playing
- Clarify that r? works in comments.

## embedded-book

1 commits in 8c395bdd8073deb20ca67e1ed4b14a3a7e315a37..d5fc1bce3f8eb398f9c25f1b15e0257d7537cd41
2021-11-14 11:38:31 +0000 to 2022-01-24 07:13:31 +0000
- Add link to Japanese translation  (rust-embedded/book#311)
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.

4 participants