pattern

Rust Reference patterns and matching through a borrow

A reference pattern matches the reference layer explicitly. In this example the referenced integer implements Copy, so the binding is an integer value rather than another reference. Expected output (not executed): 10 9 Why: The pattern copies the integer out of the shared reference. Common mistake: Do not generalize this to moving a non-Copy String out of a shared reference.

Formal Syntax & Structure

rust syntaxrust
let &binding = reference;

Working Example

Example Snippetrust
fn main() {    let points = 9;    let borrowed = &points;    let &copy = borrowed;    println!("{} {}", copy + 1, points);}
Rust Reference