The following keywords currently have the functionality described.
- - perform primitive casting, disambiguate the specific trait containing an item, or rename items in
use
andextern crate
statements async
- return aFuture
instead of blocking the current threadawait
- suspend execution until the result of aFuture
is readybreak
- 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 defineddyn
- dynamic dispatch to a trait objectelse
- 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 lifetimeimpl
- implement inherent or trait functionalityin
- part offor
loop syntax- - bind a variable
loop
- 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 we are defining or implementingself
- 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 traittype
- define a type alias or associated typeunion
- define a and is only a keyword when used in a union declarationunsafe
- denote unsafe code, functions, traits, or implementationsuse
- bring symbols into scopewhere
- denote clauses that constrain a typewhile
- loop conditionally based on the result of an expression
The following keywords do not have any functionality but are reserved by Rust for potential future use.
abstract
box
do
final
macro
override
priv
try
typeof
unsized
yield
Raw identifiers are the syntax that lets you use keywords where they wouldn’t normally be allowed. You use a raw identifier by prefixing a keyword with r#
.
Filename: src/main.rs
fn match(needle: &str, haystack: &str) -> bool { haystack.contains(needle) }
you’ll get this error:
The error shows that you can’t use the keyword match
as the function identifier. To use match
as a function name, you need to use the raw identifier syntax, like this:
Filename: src/main.rs
This code will compile without any errors. Note the r#
prefix on the function name in its definition as well as where the function is called in main
.