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

KeywordDescriptionC/C++ Equivalent
asType casting or renaming importstypedef, as in C++
asyncDefines asynchronous functionsNone (C++20 has co_await)
awaitAwaits the result of an asynchronous operationNone (C++20 co_await)
breakExits loops or blocks earlybreak
constDefines a constant valueconst
continueSkips the rest of the loop iterationcontinue
crateRefers to the current crate/moduleNone
elseFollows an if block with an alternative branchelse
enumDefines an enumerationenum
externDeclares external language functions or dataextern
falseBoolean false literalfalse
fnDefines a functionvoid, int, etc. in C
forDefines a loop over iteratorsfor
ifConditional code executionif
implDefines implementations for traits or typesNone
inUsed in for loop to iterate over elements(C++ range-for)
letDefines a variableNo direct equivalent
loopCreates an infinite loopwhile (true)
matchPattern matchingswitch in C/C++
modDeclares a moduleNone
moveForces closure to take ownership of variablesNone
mutDeclares a mutable variableNo direct equivalent
pubMakes an item public (visibility modifier)public in C++
refRefers to a reference in pattern matchingC++ & (reference types)
returnExits from a function with a valuereturn
selfRefers to the current instance of an object or moduleC++ this
staticDeclares a static variable or lifetimestatic
structDefines a structurestruct
traitDefines a trait (similar to interfaces)C++ abstract classes
trueBoolean true literaltrue
typeDefines an alias or associated typetypedef
unsafeAllows code that bypasses Rust’s safety checksNone (unsafe C inherently)
useBrings items into scope from other modules#include, using in C++
whereSpecifies conditions for genericsNone
whileDefines a loop with a conditionwhile

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 KeywordC/C++ Equivalent
abstractabstract (C++)
becomeNone
boxNone
dodo (C)
finalfinal (C++)
macroNone
overrideoverride (C++)
privprivate (C++)
trytry (C++)
typeoftypeof (C++)
unsizedNone
virtualvirtual (C++)
yieldyield (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.