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

Tutorial ambiguity or mistake re: destructuring & let #11113

Closed
olliebun opened this issue Dec 22, 2013 · 7 comments · Fixed by #15121
Closed

Tutorial ambiguity or mistake re: destructuring & let #11113

olliebun opened this issue Dec 22, 2013 · 7 comments · Fixed by #15121

Comments

@olliebun
Copy link

From the tutorial:

You've already seen simple let bindings, but let is a little fancier than you've been led to believe. It, too, supports destructuring patterns. For example, you can write this to extract the fields from a tuple, introducing two variables at once: a and b.

let (a, b) = get_tuple_of_two_ints();

Let bindings only work with irrefutable patterns: that is, patterns that can never fail to match. This excludes let from matching literals and most enum variants.

(emphasis mine)

This code compiles and behaves as expected:

let (a, b) = (1,2);

Have not included a pull request, as I'm not sure what the meaning of the passage is otherwise.

@glaebhoerl
Copy link
Contributor

That does, but this doesn't:

let (a, 2) = (1, 2);

which is the intended meaning.

Can you suggest a phrasing which would've been unambiguous for you?

@jhasse
Copy link
Contributor

jhasse commented Dec 22, 2013

How about simply removing the last sentence? That let (a, 2) = (1, 2) doesn't work shouldn't need explaining IMHO.

@olliebun
Copy link
Author

Tricky - this seems to suggest that you can destructure literals with let bindings, just that you can't do so into a pattern that exists in a match. Is that right?

@glaebhoerl
Copy link
Contributor

Hmm? The thing being matched (on the RHS of let, or directly following match) does not matter at all. What matters is that in a let, on the LHS, you can only have a single pattern which must match any possible value of the type. If you need more than one pattern to cover all the cases, you need to use a match.

@pnkfelix
Copy link
Member

part of #11755

@steveklabnik
Copy link
Member

I've made a PR to improve this. Thoughts?

@olliebun
Copy link
Author

Yep that makes sense.

steveklabnik added a commit to steveklabnik/rust that referenced this issue Jun 25, 2014
flip1995 pushed a commit to flip1995/rust that referenced this issue Jul 14, 2023
…ity, r=Manishearth

Set `unnecessary_cast` suggestion to `MaybeIncorrect` for pointer casts

Closes rust-lang#11113

changelog: none
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants