Skip to content

Closures #1746

@junderw

Description

@junderw
Contributor

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!

Activity

mo8it

mo8it commented on Sep 2, 2024

@mo8it
Contributor

@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.

changed the title [-]Would you be willing to include lessons on capturing (closures / async blocks) and the `Fn*` traits?[/-] [+]Closures[/+] on Sep 2, 2024
added this to the v7 milestone on Sep 2, 2024
junderw

junderw commented on Sep 2, 2024

@junderw
ContributorAuthor

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

divyaranjan1905 commented on Sep 2, 2024

@divyaranjan1905

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

mo8it commented on Sep 2, 2024

@mo8it
Contributor

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

divyaranjan1905 commented on Sep 2, 2024

@divyaranjan1905

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.

linked a pull request that will close this issue on Sep 2, 2024
junderw

junderw commented on Sep 2, 2024

@junderw
ContributorAuthor

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

rikettsie commented on Sep 30, 2024

@rikettsie

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

junderw commented on Nov 1, 2024

@junderw
ContributorAuthor

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

      Development

      Participants

      @rikettsie@junderw@divyaranjan1905@mo8it

      Issue actions

        Closures · Issue #1746 · rust-lang/rustlings