Common Syntax
| Purpose | Syntax | Example |
|---|---|---|
| Create a variable | let name: Type = value; | let age: i32 = 25; |
| Mutable variable | let mut name: Type = value; | let mut score = 0; |
| Constant | const NAME: Type = value; | const PI: f64 = 3.14; |
| Function | fn name(param: Type) { } | fn greet(name: &str) { } |
| Function with return | fn name() -> Type { } | fn add() -> i32 { } |
| Return a value | last expression or return | x + y |
| If statement | if condition { } | if age >= 18 { } |
| Else if | else if condition { } | else if age == 18 { } |
| Else | else { } | else { println!("No"); } |
| Match | match value { } | match n { 1 => ..., _ => ... } |
| Loop forever | loop { } | loop { break; } |
| While loop | while condition { } | while x < 10 { } |
| For loop | for item in collection { } | for n in 1..5 { } |
| Struct | struct Name { } | struct User { age: u32 } |
| Enum | enum Name { } | enum Color { Red, Blue } |
| Method | impl Type { fn method(...) { } } | impl User { fn greet(&self) {} } |
| Type casting | value as Type | count as u64 |
| Reference | &value | &name |
| Mutable reference | &mut value | &mut score |
| Generic type | Type<T> | Vec<String> |
| Generic function | fn name<T>() | fn print<T>(value: T) |
| Trait implementation | impl Trait for Type | impl Display for User |
| Error propagation | ? | file.read_to_string()?; |
Rust Symbols & Syntax
| Symbol | Name | Common use |
|---|---|---|
| () | Parentheses | Function calls, parameters, unit type |
| {} | Curly braces | Blocks, functions, structs, match arms |
| [] | Square brackets | Arrays, indexing, attributes (#[derive]) |
| <> | Angle brackets | Generics (Vec<T>, Option<T>) |
| : | Colon | Type annotations (x: i32) |
| :: | Double colon | Modules and namespaces (std::io::stdin) |
| ; | Semicolon | Ends a statement |
| , | Comma | Separates items |
| . | Dot | Field and method access (user.name) |
| .. | Range operator | 1..5, struct update syntax |
| ..= | Inclusive range | 1..=5 |
| => | Fat arrow | Match arms |
| -> | Thin arrow | Function return type |
| = | Assignment | Assign a value |
| == | Equality | Compare values |
| != | Not equal | Compare values |
| > | Greater than | Comparison |
| < | Less than | Comparison, generics |
| >= | Greater than or equal | Comparison |
| <= | Less than or equal | Comparison |
| + | Plus | Addition |
| - | Minus | Subtraction |
| * | Asterisk | Multiplication, dereference |
| / | Slash | Division |
| % | Modulo | Remainder |
| += | Add assignment | x += 1 |
| -= | Subtract assignment | x -= 1 |
| *= | Multiply assignment | x *= 2 |
| /= | Divide assignment | x /= 2 |
| & | Ampersand | Immutable reference (borrow) |
| &mut | Mutable reference | Mutable borrow |
| && | Double ampersand | Logical AND |
| || | Double pipe | Logical OR |
| ! | Exclamation mark | Macros (println!), logical NOT, never type |
| ? | Question mark | Error propagation |
| _ | Underscore | Ignore a value, wildcard pattern |
| @ | At symbol | Pattern binding in match |
| | | Pipe | Closures, pattern alternatives |
| #[] | Attribute | #[derive(Debug)], #[test] |
Common Rust Macros
| Macro | Purpose | Example |
|---|---|---|
println! | Print with newline | println!("Hello"); |
print! | Print without newline | print!("Hello"); |
dbg! | Debug a value | dbg!(value); |
assert! | Assert a condition | assert!(x > 0); |
assert_eq! | Assert equality | assert_eq!(x, 5); |
assert_ne! | Assert inequality | assert_ne!(x, 0); |
vec! | Create a vector | vec![1, 2, 3] |
format! | Format into a String | format!("Hi {}", name) |
panic! | Stop execution | panic!("Something went wrong"); |