Open junderw opened 1 year ago
@divyaranjan1905 also suggested to add a set of exercises about closures in https://github.com/rust-lang/rustlings/issues/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 https://github.com/rust-lang/rustlings/issues/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.
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.
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.
You are right, I forgot about this PR: https://github.com/rust-lang/rustlings/pull/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?
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 withFnMut
and one withFnOnce
. 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.
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.
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 _movesemantics), 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.
I rebased and added a closures2.rs to the PR in #2099
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:
move
move
(including ie. binding a reference to a variable before using it in the closure and a reference is moved)Fn*
traits are auto-implemented.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!