Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

tutorial: Compare closure argument patterns to match statements #4417

Closed
brson opened this Issue · 7 comments

4 participants

@brson
Owner

The explanation of argument patterns in closures is pretty dense. Maybe it could be explained better by comparing to a match statement.

See http://www.reddit.com/r/rust/comments/16b9wh/tutorial_question_destructuring_in_stack_closures/

@cmr
Collaborator
cmr commented

And also let patterns

@pnkfelix
Collaborator

part of #11755

@steveklabnik

So much of this has changed, both with for, and with closures, that I don't know if this issue is still relevant. is it?

@pnkfelix
Collaborator

@steveklabnik there are a family of potential problems that are flagged in the reddit discussion. Here are the ones that I can identify with a quick skim:

  • The original poster did not see that the syntax &x within the closure's arguments was a pattern binding that performs a dereference of the input,
  • The fact that the closure itself takes a reference as input might be obscured by the fact that the closure doesn't have an explicit type signature (at least, that was one of huon's theories in his comment),
  • brson interpreted the discussion as a sign that drawing an analogy between match and the arguments to a fn could be helpful. (But !kibwen's comment points out that the patterns for fn arguments need to be irrefutable, which is an important distinction and thus I am not 100% sure an analogy with match will help.)

I'm not sure if this particular ticket is actionable on its own.

I would want to make sure that the guide covers the fact that &x means one thing in expression context (namely, "make a reference to the l-value x") and another thing in pattern context (namely, "dereference the input and bind the resulting value to x"). We cannot put that into the "Variable bindings" section of the current guide, since that precedes the section on "Pointers", but maybe just make sure that there is a TODO in the "Pointers" section that ensures we cover the distinction between an &x expression and an &x pattern.

@steveklabnik

maybe just make sure that there is a TODO in the "Pointers" section that ensures we cover the distinction between an &x expression and an &x pattern.

Cool. Agreed. Since I'm working on pointers right now, that's very timely!

cc #16119

@steveklabnik

I actually think I'm just going to make patterns their own section, after pointers. We haven't talked about anything more than just very basic usage, and ref at the very least deserves an explanation

@pnkfelix
Collaborator

@steveklabnik that is probably best; there are a lot of subtle details in terms of the distinction between how l-values are handled versus r-values.

@steveklabnik steveklabnik referenced this issue from a commit in steveklabnik/rust
@steveklabnik steveklabnik Guide: patterns.
Fixes #4417.
645e22c
@steveklabnik steveklabnik referenced this issue from a commit in steveklabnik/rust
@steveklabnik steveklabnik Guide: patterns.
Fixes #4417.
d048cc5
@steveklabnik steveklabnik referenced this issue from a commit in steveklabnik/rust
@steveklabnik steveklabnik Guide: patterns.
Fixes #4417.
d97ed69
@steveklabnik steveklabnik referenced this issue from a commit in steveklabnik/rust
@steveklabnik steveklabnik Guide: patterns.
Fixes #4417.
b8e2e64
@steveklabnik steveklabnik referenced this issue from a commit in steveklabnik/rust
@steveklabnik steveklabnik Guide: patterns.
Fixes #4417.
2081f33
@steveklabnik steveklabnik referenced this issue from a commit in steveklabnik/rust
@steveklabnik steveklabnik Guide: patterns.
Fixes #4417.
e1d9d97
@steveklabnik steveklabnik referenced this issue from a commit in steveklabnik/rust
@steveklabnik steveklabnik Guide: patterns.
Fixes #4417.
ee3f074
@bors bors closed this in #16257
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.