rust-lang / reference

The Rust Reference
https://doc.rust-lang.org/nightly/reference/
Apache License 2.0
1.25k stars 491 forks source link

Fix wildcard pattern documentation #1576

Closed Tomer-Eliahu closed 3 months ago

Tomer-Eliahu commented 3 months ago

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 commented 3 months ago

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 commented 3 months ago

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.