let x = 5; | Immutable variable |
let mut x = 5; | Mutable variable |
const MAX: i32 = 100; | Constant |
let x: i32 = 5; | Type annotation |
i8, i16, i32, i64, i128, isize | Signed integers |
u8, u16, u32, u64, u128, usize | Unsigned integers |
f32, f64 | Floating point |
bool | Boolean |
char | Unicode character |
&str / String | String slice / owned string |
+ - * / % | Arithmetic |
== != < > <= >= | Comparison |
&& || ! | Logical |
& | ^ << >> | Bitwise |
&x | Reference |
*x | Dereference |
.. | Range (exclusive) |
..= | Range (inclusive) |
let s2 = s1; | Move ownership |
let s2 = s1.clone(); | Deep copy |
&T | Immutable reference |
&mut T | Mutable reference |
fn f(s: &String) {} | Borrowing parameter |
fn f(s: &mut String) {} | Mutable borrow |
&'a T | Reference with lifetime |
fn f<'a>(x: &'a str) -> &'a str | Lifetime annotation |
&'static str | Static lifetime |
struct S<'a> { x: &'a str } | Struct with lifetime |
if cond { } else { } | If-else |
let x = if cond { a } else { b }; | If expression |
if let Some(x) = opt { } | If let |
while let Some(x) = iter.next() { } | While let |
loop { } | Infinite loop |
while cond { } | While loop |
for x in iter { } | For loop |
for i in 0..10 { } | Range loop |
break / continue | Loop control |
break 'label value | Break with value |
'label: loop { } | Labeled loop |
match x { 1 => a, 2 => b, _ => c } | Match expression |
1 | 2 | 3 => ... | Multiple patterns |
1..=5 => ... | Range pattern |
Some(x) => ... | Destructure enum |
if guard => ... | Match guard |
@ binding | Bind to variable |
let v: Vec<i32> = Vec::new(); | Create vector |
let v = vec![1, 2, 3]; | Vector macro |
v.push(x) / v.pop() | Add/remove |
v[i] / v.get(i) | Access element |
&v[..] / &v[1..3] | Slice |
HashMap::new() | Create HashMap |
map.insert(k, v) | Insert to map |
map.get(&k) | Get from map |
HashSet::new() | Create HashSet |
struct Name { field: Type } | Named struct |
struct Point(i32, i32); | Tuple struct |
struct Unit; | Unit struct |
Name { field: value } | Instantiate |
Name { field, ..other } | Struct update |
enum E { A, B(i32), C { x: i32 } } | Enum variants |
Option<T>: Some(x) / None | Option type |
Result<T, E>: Ok(x) / Err(e) | Result type |
fn name(a: i32, b: i32) -> i32 { } | Function definition |
fn name() { } // returns () | No return (unit) |
expression // implicit return | Implicit return |
return value; | Explicit return |
|x| x + 1 | Closure |
|x: i32| -> i32 { x + 1 } | Typed closure |
move || { } | Move closure |
impl Name { fn method(&self) { } } | Method definition |
&self | Immutable self |
&mut self | Mutable self |
self | Consuming self |
Self | Type alias for impl type |
fn new() -> Self { } | Associated function |
trait Name { fn method(&self); } | Define trait |
impl Trait for Type { } | Implement trait |
fn f(x: impl Trait) | Impl trait parameter |
fn f(x: &dyn Trait) | Trait object |
Box<dyn Trait> | Boxed trait object |
#[derive(Debug, Clone)] | Derive macro |
fn f<T>(x: T) { } | Generic function |
struct S<T> { field: T } | Generic struct |
fn f<T: Trait>(x: T) | Trait bound |
fn f<T: A + B>(x: T) | Multiple bounds |
where T: Trait | Where clause |
impl<T> S<T> { } | Generic impl |
result? | Propagate error |
result.unwrap() | Panic if Err |
result.expect("msg") | Panic with message |
result.unwrap_or(default) | Default value |
result.unwrap_or_else(|| ...) | Lazy default |
option.map(|x| ...) | Transform value |
option.and_then(|x| ...) | Flat map |
option.ok_or(err) | Option to Result |
#[derive(Debug)] | Derive Debug |
impl std::error::Error for E { } | Implement Error |
impl std::fmt::Display for E { } | Implement Display |
thiserror::Error | thiserror crate |
anyhow::Error | anyhow crate |
cargo new project | Create new project |
cargo build | Build project |
cargo build --release | Build optimized |
cargo run | Run project |
cargo test | Run tests |
cargo check | Check without building |
cargo clippy | Lint code |
cargo fmt | Format code |
cargo doc --open | Generate docs |
cargo add crate | Add dependency |
clippy and rustfmt for consistent code&str over String in function parameters? operator for error propagationmatch or if let over unwrap()derive for common traitsOption instead of null values