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

Closures #1746

Open
junderw opened this issue Oct 29, 2023 · 8 comments · May be fixed by #2099
Open

Closures #1746

junderw opened this issue Oct 29, 2023 · 8 comments · May be fixed by #2099
Labels
A-exercises Area: Exercises C-new-exercise Category: New Exercise P-medium Priority: Medium
Milestone

Comments

@junderw
Copy link
Contributor

junderw commented Oct 29, 2023

One topic I see new people hit pretty early on is the capturing semantics and the Fn* traits.

I was thinking of adding a few lessons to show:

  1. Capturing semantics without move
  2. Capturing semantics with move (including ie. binding a reference to a variable before using it in the closure and a reference is moved)
  3. How the Fn* traits are auto-implemented.
  4. Briefly mention that async block capturing is similar to closure capturing, but that there is no Fn* traits for async-block-futures.

I wanted to read the room a bit before actually putting time into this. Do we think this might be a bit too advanced for rustlings? Would it depend on the actual content? Would anyone be willing to help review my lessons to try and get it over the finish line?

It's an issue I get asked about constantly, especially when dealing with GUI frameworks like egui that make use of lots of nested closures... it might help people intuit these issues more clearly.

Let me know what you think!

@mo8it
Copy link
Contributor

mo8it commented Sep 2, 2024

@divyaranjan1905 also suggested to add a set of exercises about closures in #2096. Capturing is one part of closures. So you are welcome to join forces and create a PR :D

I won't include async in these exercises since Rustlings doesn't teach async yet. I am waiting for the new async chapter to be included in the official Rust book to create a set of async exercises then. See #2040

Keep in mind that I won't merge the PR until I want to publish version 7. So I can review it now and automatically merge it later before the next major release.

@mo8it mo8it changed the title Would you be willing to include lessons on capturing (closures / async blocks) and the Fn* traits? Closures Sep 2, 2024
@mo8it mo8it added this to the v7 milestone Sep 2, 2024
@mo8it mo8it added A-exercises Area: Exercises P-medium Priority: Medium C-new-exercise Category: New Exercise labels Sep 2, 2024
@junderw
Copy link
Contributor Author

junderw commented Sep 2, 2024

I think there’s an open PR that adds a bit of closures to the functions lesson.

I feel like that is a good idea to get the student at least a little bit used to closures before going into more depth.

I think closures should be explained after traits and tests. This is in line with the Rust book as well.

@divyaranjan1905
Copy link

I think closures should be explained after traits and tests. This is in line with the Rust book as well.

Indeed, as I proposed in the issue, it would replace the currently 18th set (iterators), so would be after traits and tests.

@mo8it
Copy link
Contributor

mo8it commented Sep 2, 2024

You are right, I forgot about this PR: #1748

I agree @junderw, that PR only shows the basics. After talking about traits, I would like to have 3 additional exercises where you need to write your own functions that take a closure, one with Fn, one with FnMut and one with FnOnce. What do you think?

@divyaranjan1905
Copy link

After talking about traits, I would like to have 3 additional exercises where you need to write your own functions that take a closure, one with Fn, one with FnMut and one with FnOnce. What do you think?

Yep, @mo8it I think that'll be in line with the chapter from The Book. I'm considering to keep the number of exercises to 4, or at most 5.

@junderw junderw linked a pull request Sep 2, 2024 that will close this issue
@junderw
Copy link
Contributor Author

junderw commented Sep 2, 2024

I was actually thinking of going a bit more in-depth with closures.

Not too deep, but deep enough to give people a good overview of capturing, the Fn traits and how they're implemented automatically, and other things.

Since Closures are a pretty important part of many aspects of Rust, I think it warrants its own folder.

I have created a sample exercise to get started.

If you have a timeline for v7 I can try to find some time before then.

Thanks.

@rikettsie
Copy link

rikettsie commented Sep 30, 2024

Actually @divyaranjan1905, @junderw , when I wrote that couple of exercises last year (#1748), I considered them a gentle introduction to closures.

I still think it's a good idea to have a taste of closures early in those chapters (functions and move_semantics), and definitely a very good thing to add a dedicated chapter later to build a deeper understanding (probably between traits and iterators).

@mo8it, I updated the PR #1748 to compile to the new rustlings standard, if you think it's valuable to merge.

@junderw
Copy link
Contributor Author

junderw commented Nov 1, 2024

I rebased and added a closures2.rs to the PR in #2099

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-exercises Area: Exercises C-new-exercise Category: New Exercise P-medium Priority: Medium
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants