Appendix A: Keywords
The following list contains keywords that are reserved for current or future use by the Rust language. As such, they cannot be used as identifiers (except as raw identifiers), including names of functions, variables, parameters, struct fields, modules, crates, constants, macros, static values, attributes, types, traits, or lifetimes.
Keywords Currently in Use
The following keywords currently have the functionality described.
as
- perform primitive casting, disambiguate the specific trait containing an item, or rename items inuse
andextern crate
statementsbreak
- exit a loop immediatelyconst
- define constant items or constant raw pointerscontinue
- continue to the next loop iterationcrate
- link an external crate or a macro variable representing the crate in which the macro is definedelse
- fallback forif
andif let
control flow constructsenum
- define an enumerationextern
- link an external crate, function, or variablefalse
- Boolean false literalfn
- define a function or the function pointer typefor
- loop over items from an iterator, implement a trait, or specify a higher-ranked lifetimeif
- branch based on the result of a conditional expressionimpl
- implement inherent or trait functionalityin
- part offor
loop syntaxlet
- bind a variableloop
- loop unconditionallymatch
- match a value to patternsmod
- define a modulemove
- make a closure take ownership of all its capturesmut
- denote mutability in references, raw pointers, or pattern bindingspub
- denote public visibility in struct fields,impl
blocks, or modulesref
- bind by referencereturn
- return from functionSelf
- a type alias for the type implementing a traitself
- method subject or current modulestatic
- global variable or lifetime lasting the entire program executionstruct
- define a structuresuper
- parent module of the current moduletrait
- define a traittrue
- Boolean true literaltype
- define a type alias or associated typeunsafe
- denote unsafe code, functions, traits, or implementationsuse
- import symbols into scopewhere
- denote clauses that constrain a typewhile
- loop conditionally based on the result of an expression
Keywords Reserved for Future Use
The following keywords do not have any functionality but are reserved by Rust for potential future use.
abstract
alignof
become
box
do
final
macro
offsetof
override
priv
proc
pure
sizeof
typeof
unsized
virtual
yield
Raw identifiers
Raw identifiers let you use keywords where they would not normally be allowed by
prefixing them with r#
.
For example, match
is a keyword. If you try to compile this function:
fn match(needle: &str, haystack: &str) -> bool {
haystack.contains(needle)
}
You'll get this error:
error: expected identifier, found keyword `match`
--> src/main.rs:4:4
|
4 | fn match(needle: &str, haystack: &str) -> bool {
| ^^^^^ expected identifier, found keyword
You can write this with a raw identifier:
fn r#match(needle: &str, haystack: &str) -> bool { haystack.contains(needle) } fn main() { assert!(r#match("foo", "foobar")); }
Note the r#
prefix on both the function name as well as the call.
Motivation
This feature is useful for a few reasons, but the primary motivation was
inter-edition situations. For example, try
is not a keyword in the 2015
edition, but is in the 2018 edition. So if you have a library that is written
in Rust 2015 and has a try
function, to call it in Rust 2018, you'll need
to use the raw identifier.