5.1 Keywords
Keywords are an integral part of any programming language, including Rust. They are reserved words that have a specific meaning to the compiler and cannot be used for variable names, function names, or any other identifiers in your programs. Keywords define the structure and behavior of your code, from flow control to data declarations and memory management.
Rust has a unique set of keywords that you’ll see frequently as you write Rust programs. Some of these keywords will look familiar if you come from a C or C++ background, while others might be new. It’s important to understand that while Rust shares some similarities with C, it also introduces concepts that are specific to memory safety and concurrency, which are reflected in its keyword set.
Additionally, Rust provides a special feature called raw identifiers, which allow you to use keywords as regular identifiers by prefixing them with r#
. This is particularly useful when interfacing with C code, where certain keywords may conflict with variable names or function names from other languages. For example:
#![allow(unused)] fn main() { let r#struct = 5; // 'struct' is a keyword in Rust, but here it's used as a regular variable name println!("The value is {}", r#struct); }
Below, we’ll list the Rust keywords that are currently in use, along with a separate list of reserved keywords that may be used in the future. We’ll also draw comparisons to C and C++ where relevant.
5.1.1 Rust Keywords
Keyword | Description | C/C++ Equivalent |
---|---|---|
as | Type casting or renaming imports | typedef , as in C++ |
async | Defines asynchronous functions | None (C++20 has co_await ) |
await | Awaits the result of an asynchronous operation | None (C++20 co_await ) |
break | Exits loops or blocks early | break |
const | Defines a constant value | const |
continue | Skips the rest of the loop iteration | continue |
crate | Refers to the current crate/module | None |
else | Follows an if block with an alternative branch | else |
enum | Defines an enumeration | enum |
extern | Declares external language functions or data | extern |
false | Boolean false literal | false |
fn | Defines a function | void , int , etc. in C |
for | Defines a loop over iterators | for |
if | Conditional code execution | if |
impl | Defines implementations for traits or types | None |
in | Used in for loop to iterate over elements | (C++ range-for ) |
let | Defines a variable | No direct equivalent |
loop | Creates an infinite loop | while (true) |
match | Pattern matching | switch in C/C++ |
mod | Declares a module | None |
move | Forces closure to take ownership of variables | None |
mut | Declares a mutable variable | No direct equivalent |
pub | Makes an item public (visibility modifier) | public in C++ |
ref | Refers to a reference in pattern matching | C++ & (reference types) |
return | Exits from a function with a value | return |
self | Refers to the current instance of an object or module | C++ this |
static | Declares a static variable or lifetime | static |
struct | Defines a structure | struct |
trait | Defines a trait (similar to interfaces) | C++ abstract classes |
true | Boolean true literal | true |
type | Defines an alias or associated type | typedef |
unsafe | Allows code that bypasses Rust’s safety checks | None (unsafe C inherently) |
use | Brings items into scope from other modules | #include , using in C++ |
where | Specifies conditions for generics | None |
while | Defines a loop with a condition | while |
5.1.2 Reserved Keywords (For Future Use)
Rust also reserves certain keywords that aren’t currently in use but may be added in future language versions. These cannot be used as identifiers even though they have no current functionality.
Reserved Keyword | C/C++ Equivalent |
---|---|
abstract | abstract (C++) |
become | None |
box | None |
do | do (C) |
final | final (C++) |
macro | None |
override | override (C++) |
priv | private (C++) |
try | try (C++) |
typeof | typeof (C++) |
unsized | None |
virtual | virtual (C++) |
yield | yield (C++) |
5.1.3 Comparison to C/C++
In many cases, Rust keywords will look familiar to those coming from C or C++. For example, if
, else
, while
, for
, and return
function much as they do in C. However, Rust introduces new concepts that have no direct equivalent in C/C++, such as async
, await
, match
, trait
, and unsafe
. These keywords reflect Rust’s design priorities around safety, concurrency, and pattern matching.
One of the most significant differences is Rust’s concept of ownership and the associated keywords like mut
, move
, and ref
, which are designed to ensure memory safety at compile time. In C and C++, memory management is largely manual and prone to errors, whereas Rust’s keywords enforce strict borrowing rules to avoid issues like dangling pointers or data races.
Understanding the set of keywords in Rust is key to mastering the language and writing safe, efficient, and expressive code.