rust-lang / nomicon

The Dark Arts of Advanced and Unsafe Rust Programming
https://doc.rust-lang.org/nomicon/
Apache License 2.0
1.75k stars 258 forks source link

Clarify the HRTB chapter #330

Closed fmease closed 2 years ago

fmease commented 2 years ago

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

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.

dcormier commented 2 years ago

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.

Gankra commented 2 years ago

thanks!