5

The title might not be appropriate, be here is an example:

fn foo(impl Fn(&u32) -> bool) { ... }

foo(|x| *x < 100);
foo(|&x| x < 100);

Are the two closures passed to foo equivalent? I saw people use the second form in some places, but I couldn't find it in the official book. Is the &x part a destructure...?

Zizheng Tai
  • 5,622
  • 21
  • 70
  • Hi there! I closed this as dupe as this question has been asked a bunch of time already. In short: yes `&x` is part of a destructure and is equivalent to dereferencing (only works for `Copy` types). If you don't think the duplicates are correct as they don't answer your question, please let us know! – Lukas Kalbertodt Sep 07 '19 at 09:08

1 Answers1

2

This is called reference pattern:

ReferencePattern :

(&|&&) mut? Pattern

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

So yes, they are equivalent.

Community
  • 1
  • 1
edwardw
  • 10,936
  • 3
  • 31
  • 46