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

Simplify note about functions in ch13-01-closures.md #3699

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

proski
Copy link

@proski proski commented Jul 13, 2023

No description provided.

@proski
Copy link
Author

proski commented Jul 13, 2023

The note was very hard to read, I think I had to read it about 5 times before I could fully understand it. I'm still learning Rust, so maybe it's easier for others.

Changes I made:

  • I made it clear that the Fn traits are implemented implicitly, i.e. no extra code is needed.
  • I used shorter sentences with simpler word order.
  • I reordered the sentences: statement, example, explanation, additional considerations.
  • I used scope instead of environment as environment has some irrelevant meanings (e.g. environment variables).

I also considered adding an example for Result, where a function taking one argument could be used. That would illustrate that taking arguments is not "capturing". However, I decided against it to keep the note simple.

@proski proski force-pushed the closure-note branch 2 times, most recently from efd95da to 56825f0 Compare July 18, 2023 19:46
@proski
Copy link
Author

proski commented Jul 18, 2023

I've restored environment, it's used consistently in the text and it wasn't the reason the note was hard to read. I also removed the word implicitly I used in the previous version of this PR. It's actually worth noting that the Fn traits are defined implicitly by the compiler (i.e. without any code), but that text doesn't belong to the note about functions.

@chriskrycho
Copy link
Contributor

Thanks for the suggestion here. I spent some time looking at this and at the original text, and while I understand why it may have been confusing, I think the original text is actually somewhat clearer here. The main thing I am still mulling on is whether we should clarify that functions implementing those traits happens automatically, i.e. the compiler does it for you as appropriate. Something like this, maybe:

Note: Functions can implement all three of the Fn traits too. If what we want to do doesn’t require capturing a value from the environment, we can use the name of a function rather than a closure where we need something that implements one of the Fn traits. For example, on an Option<Vec<T>> value, we could call unwrap_or_else(Vec::new) to get a new, empty vector if the value is None. The compiler implements the Fn traits correctly and automatically for functions.

@chriskrycho chriskrycho added this to the ch13 milestone Apr 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants