Rust Keywords & Operators
Keywords are words the Rust grammar reserves for itself. A keyword can never be an ordinary name.
The operators in one place
One small line each: maths, comparing, and logic. This lesson gathers them in one place.
let total = 6 + 4; // add: 10
let leftover = 10 % 3; // remainder: 1
let at_least = total >= 10; // compare: true
let both = at_least && leftover == 1; // and: true
println!("total = {total}, leftover = {leftover}, both = {both}");Ranges count for you
Two dots make a range that stops before the end. Add an equals sign and the end is included too.
for n in 1..5 { println!("{n}"); } // stops before 5
for n in 1..=3 { println!("{n}"); } // the = includes the 3Change a value in place
The += and -= signs fold add-or-take-away and save into one step:
let mut score = 10;
score += 5; // same as score = score + 5
score -= 3; // same as score = score - 3
println!("score = {score}");Borrow a keyword as a name
Prefix a keyword with r# and it becomes an ordinary name — a raw identifier. Write the r# at the definition and at every use:
let r#type = "raw identifier";
println!("r#type = {}", r#type);A weak keyword is only sometimes special
union is a keyword only inside a union declaration. Everywhere else it is an ordinary name — no r# needed:
let union = 5;
println!("union = {union}");The turbofish pins a type
When Rust cannot guess which type you want, ::<> — the turbofish — says it out loud, right in the expression:
let number = "42".parse::<i32>().unwrap();
println!("parsed = {number}");Example
fn main() {
let r#match = "match is a keyword, so we escape it";
println!("{}", r#match);
let mut count = 0;
loop { count += 1; if count == 3 { break; } }
println!("counted to {}", count);
}Key points
- A keyword is a word the grammar reserves
- It can never be an ordinary name for a variable or function
- It cannot name a field, type, or other identifier either
- Keywords in use today include: as, async, await, break, const, continue, crate, dyn, else, enum, extern, false, fn, for, if, impl, in, let, loop, match, mod, move, mut, pub, ref, return, self, Self, static, struct, super, trait, true, type, unsafe, use, where, while
- Reserved keywords are saved for future features
- They are banned as names today: abstract, become, box, do, final, gen, macro, override, priv, try, typeof, unsized, virtual, yield
- Reserving early protects you
- When a reserved word one day gains a meaning, no existing program was using it as a name
- So nothing breaks
- Editions grow this reserved list slowly
- try became a reserved keyword in 2018
- gen followed in 2024
- So a name that is legal in one edition can be a keyword in the next
In this example, the variable is named match, a keyword. So it is written as the raw identifier r#match. That spelling appears where it is created and where println! reads it. Its value prints as plain text: match is a keyword, so we escape it. The rest of the program is ordinary. A loop adds 1 to count each round. It breaks when count reaches 3. So the last line prints counted to 3. The keywords let, loop, if and break do their normal jobs around the one borrowed name.
Note
Never memorise this list to write code — the compiler names the keyword in its error
The list earns its place when you design names for a library that other people must type
Try it Yourself
Edit the code, then press Run.
Running is simulated · Rust Playground
Exercise
How can you name a variable match?