pattern
Rust Match a borrowed value without moving it
When a non-reference pattern matches a reference, Rust can automatically dereference the scrutinee and adjust how names bind. This makes it possible to inspect borrowed enum data without moving an owned field. Expected output (not executed): draft true Why: text is inferred as a shared reference to the String, leaving note usable. Common mistake: Do not add explicit `ref` everywhere. Rust 2024 restricts explicit binding modifiers when the default binding mode is not move.
Formal Syntax & Structure
rust syntaxrust
match &value { Some(name) => ..., None => ... }Working Example
Example Snippetrust
fn main() { let note = Some(String::from("draft")); match ¬e { Some(text) => println!("{text}"), None => println!("empty"), } println!("{}", note.is_some());}