keyword

Rust ref: borrow a value in a pattern

`ref` is a pattern binding modifier. In an appropriate binding context it creates a shared reference instead of moving or copying the matched value into the binding. Expected output (not executed): Rust Rust Why: The pattern borrows title, so both the reference and original binding can be read. Common mistake: `ref` is used in patterns; it is not the ordinary borrow-expression operator `&`. Rust 2024 also restricts explicit binding modifiers under non-default binding modes.

Formal Syntax & Structure

rust syntaxrust
let ref name = value;

Working Example

Example Snippetrust
fn main() {    let title = String::from("Rust");    let ref view = title;    println!("{} {}", view, title);}
Rust Reference