reference icon indicating copy to clipboard operation
reference copied to clipboard

[patterns.ref.info] unclear wording: "and, thus, borrow them"

Open jofas opened this issue 6 months ago • 2 comments

The first sentence of [patterns.ref.info] states (my emphasis):

Reference patterns dereference the pointers that are being matched and, thus, borrow them.

The emphasised part reads a bit confusing to me and I'm still unsure what is meant by it. Maybe it could be clarified? Or omitted? I can't find similar terminology in the FLS.

The URLO topic that raised the issue:

https://users.rust-lang.org/t/discussing-reference-patterns/130495?u=jofas

jofas avatar Jun 08 '25 12:06 jofas

The phrase "borrow them" made me think of let (ref x, ref y) = tuple;, which feels like a different kind of borrowing than what's happening in &x patterns. Clarifying that would help.

fiveseven-lambda avatar Jul 19 '25 11:07 fiveseven-lambda

Comes from this PR; I traced it back to original developer to see whether there were intermediate steps, but that line appears as it is, and was never modified.


The text seems to conflate that the &-pattern dereferences and also borrows, but as the FLS says, it only dereferences:

A reference pattern is a pattern that dereferences a pointer that is being matched.

Matching a String under & would move it, so it fails:

fn main() {
    let s = "s".to_owned();
    let s_ref = &s;
    let &nope = s_ref;
}

iampi31415 avatar Oct 13 '25 18:10 iampi31415