Rust Raw Identifiers Introduction

RustRustBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

In this lab, we will learn about raw identifiers in Rust, which allow us to use keywords as identifiers in situations where they are typically not allowed, such as variable or function names, especially when older editions of Rust clash with newer keywords.

Note: If the lab does not specify a file name, you can use any file name you want. For example, you can use main.rs, compile and run it with rustc main.rs && ./main.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL rust(("`Rust`")) -.-> rust/FunctionsandClosuresGroup(["`Functions and Closures`"]) rust(("`Rust`")) -.-> rust/DataStructuresandEnumsGroup(["`Data Structures and Enums`"]) rust(("`Rust`")) -.-> rust/ErrorHandlingandDebuggingGroup(["`Error Handling and Debugging`"]) rust(("`Rust`")) -.-> rust/ProjectManagementandOrganizationGroup(["`Project Management and Organization`"]) rust/FunctionsandClosuresGroup -.-> rust/function_syntax("`Function Syntax`") rust/FunctionsandClosuresGroup -.-> rust/expressions_statements("`Expressions and Statements`") rust/DataStructuresandEnumsGroup -.-> rust/method_syntax("`Method Syntax`") rust/ErrorHandlingandDebuggingGroup -.-> rust/error_propagation("`Error Propagation`") rust/ProjectManagementandOrganizationGroup -.-> rust/cargo_crates("`Cargo and Crates`") subgraph Lab Skills rust/function_syntax -.-> lab-99288{{"`Rust Raw Identifiers Introduction`"}} rust/expressions_statements -.-> lab-99288{{"`Rust Raw Identifiers Introduction`"}} rust/method_syntax -.-> lab-99288{{"`Rust Raw Identifiers Introduction`"}} rust/error_propagation -.-> lab-99288{{"`Rust Raw Identifiers Introduction`"}} rust/cargo_crates -.-> lab-99288{{"`Rust Raw Identifiers Introduction`"}} end

Raw identifiers

Rust, like many programming languages, has the concept of "keywords". These identifiers mean something to the language, and so you cannot use them in places like variable names, function names, and other places. Raw identifiers let you use keywords where they would not normally be allowed. This is particularly useful when Rust introduces new keywords, and a library using an older edition of Rust has a variable or function with the same name as a keyword introduced in a newer edition.

For example, consider a crate foo compiled with the 2015 edition of Rust that exports a function named try. This keyword is reserved for a new feature in the 2018 edition, so without raw identifiers, we would have no way to name the function.

extern crate foo;

fn main() {
    foo::try();
}

You'll get this error:

error: expected identifier, found keyword `try`
 --> src/main.rs:4:4
  |
4 | foo::try();
  |      ^^^ expected identifier, found keyword

You can write this with a raw identifier:

extern crate foo;

fn main() {
    foo::r#try();
}

Summary

Congratulations! You have completed the Raw Identifiers lab. You can practice more labs in LabEx to improve your skills.

Other Rust Tutorials you may like