Skip to content
Tutorials Center
My learning

Topics

Rust Reference ยท Keywords and Statements

Raw identifiers (r#)

Reference for Rust raw identifiers: the r# prefix lets you use keywords as names, which keywords cannot be raw, edition migration and FFI use.

A raw identifier is an identifier written with the r# prefix, such as r#match or r#type. It lets you use a word that the current edition treats as a keyword as an ordinary name. The prefix is part of how you write the name; the name itself is just match or type.

Raw identifiers exist for two situations: calling into code that was written when the word was not a keyword (an older crate or edition), and mapping names from another language or data format (JSON fields called type, C functions called try).

Rust
let r#type = "user";
fn r#match(a: i32) -> i32 { a }

Try it

Rust Keywords as names

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with io::stdin().read_line()), it gets the next line from this box.

Rust Words that cannot be raw

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with io::stdin().read_line()), it gets the next line from this box.

Syntax

Rust
r#identifier
Rule Detail
Where Anywhere an identifier is allowed: variables, fields, functions, methods, types, modules and generic parameters.
Which words Any keyword, strict, reserved or edition-dependent, except crate, self, super, Self, and the lone underscore _.
Equivalence r#name and name are the same identifier when name is not a keyword.

Errors: r#self, r#crate, r#super and r#Self are rejected because those words have path meaning; the compiler reports that they cannot be a raw identifier.

Usage notes

  • Edition migration. When a new edition makes a word a keyword (async, await, dyn, try in 2018; gen in 2024), cargo fix --edition renames uses to r#word. A crate in an older edition keeps working and other crates call it with the raw form.
  • Format strings: an inline capture such as {r#type} is rejected ("raw identifiers are not supported"). Write (the compiler accepts keywords there) or pass the value as an argument: println!("{}", r#type).
  • Display and Debug: derived Debug shows the field name without r# (type: "click").
  • Macros: an $x:ident fragment accepts raw identifiers.
  • FFI and serde: use raw identifiers in structs for external data, with #[serde(rename = "type")] when the wire name differs.
  • Style: avoid raw identifiers in new APIs. kind is nicer to type than r#type.