Skip to content

Fix wildcard pattern documentation #1576

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

Closed
wants to merge 2 commits into from

Conversation

Tomer-Eliahu
Copy link

The current wildcard pattern documentation has the following as an example of the wildcard pattern:

// ignore a function/closure param
let real_part = |a: f64, _: f64| { a };

The problem is that this is not a correct example of the wildcard pattern. The wildcard pattern does not move or copy the value it matches, whereas '_' in closure (or function) parameters does move (or copy) the value. One notable example is the toilet closure:

let s = String::from("Hello");
let toilet = |_| ();
toilet(s);
println!("{}", s); //ERROR--- s was moved when we called the closure

This pull request fixes and clarifies the documentation. This also closes #848.

@traviscross
Copy link
Contributor

We talked about this in the lang-docs call today. Our feeling overall is there's an interesting question hiding in here about how to describe that the value is moved as part of the call and that the pattern doesn't matter for this.

But at the same time, this PR seems to be build around a mistake -- the idea that the _ is not a wildcard pattern in these circumstances -- so we're going to close it.

@Tomer-Eliahu
Copy link
Author

Fair enough.
I'll be happy to revise the pull request to point out that while the _ is still a wildcard pattern in function/closure calls, that the value is still moved.
For the explanation as to why that is, I think that reproducing the quote from the Function body documentation in this comment is enough.

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 this pull request may close these issues.

Wildcard pattern in function parameters.
2 participants