Rust Keywords
Essential reserved words that form Rust's syntax foundation
🔑 What are Rust Keywords?
Rust keywords are reserved words with special meanings in the language. They control program flow, define data types, and manage memory safely. Understanding keywords is essential for writing Rust code.
// Keywords in action
fn main() {
let mut x = 5;
if x > 0 {
println!("Positive number!");
}
}
Output:
Positive number!
Essential Rust Keywords
Variable Keywords
Declare and modify variables
let x = 10; // immutable
let mut y = 20; // mutable
const PI: f64 = 3.14;
Control Flow
Control program execution
if condition {
// do something
} else {
// do something else
}
Functions
Define reusable code blocks
fn greet(name: &str) -> String {
format!("Hello, {}!", name)
}
Data Types
Define custom data structures
struct Person {
name: String,
age: u32,
}
🔹 Variable Declaration Keywords
These keywords control how variables are created and modified:
fn main() {
// let - creates immutable variable
let name = "Alice";
// mut - makes variable mutable
let mut count = 0;
count += 1;
// const - compile-time constant
const MAX_SIZE: usize = 100;
// static - global variable
static GREETING: &str = "Hello, World!";
println!("{}, count: {}", name, count);
}
Output:
Alice, count: 1
🔹 Control Flow Keywords
Keywords that control how your program executes:
fn main() {
let number = 7;
// if/else - conditional execution
if number > 5 {
println!("Big number!");
} else if number > 0 {
println!("Small positive number!");
} else {
println!("Zero or negative!");
}
// match - pattern matching
match number {
1..=5 => println!("Small"),
6..=10 => println!("Medium"),
_ => println!("Large"),
}
// loop - infinite loop
let mut i = 0;
loop {
if i >= 3 { break; }
println!("Loop iteration: {}", i);
i += 1;
}
}
Output:
Big number!
Medium
Loop iteration: 0
Loop iteration: 1
Loop iteration: 2
🔹 Function and Module Keywords
Keywords for organizing and structuring code:
// fn - function definition
fn calculate(x: i32, y: i32) -> i32 {
x + y
}
// pub - public visibility
pub fn public_function() {
println!("Anyone can call this!");
}
// mod - module definition
mod math {
pub fn add(a: i32, b: i32) -> i32 {
a + b
}
}
// use - import items
use std::collections::HashMap;
fn main() {
let result = calculate(5, 3);
println!("Result: {}", result);
let sum = math::add(10, 20);
println!("Sum: {}", sum);
}
Output:
Result: 8
Sum: 30
🔹 Data Structure Keywords
Keywords for defining custom types:
// struct - custom data structure
struct Car {
brand: String,
year: u32,
}
// enum - enumeration type
enum Color {
Red,
Green,
Blue,
}
// impl - implementation block
impl Car {
fn new(brand: String, year: u32) -> Car {
Car { brand, year }
}
fn describe(&self) -> String {
format!("{} {}", self.year, self.brand)
}
}
fn main() {
let my_car = Car::new("Toyota".to_string(), 2020);
println!("My car: {}", my_car.describe());
let favorite_color = Color::Blue;
match favorite_color {
Color::Red => println!("Red like fire!"),
Color::Green => println!("Green like nature!"),
Color::Blue => println!("Blue like ocean!"),
}
}
Output:
My car: 2020 Toyota
Blue like ocean!