Rust Cheat Sheet
Rust ownership, borrowing, pattern matching, error handling with Result, and core syntax for memory-safe systems code.
2 PagesAdvancedApr 2, 2026
Basic Syntax
Variables, control flow, and printing.
rust
fn main() { let age = 30; // immutable by default let mut count = 0; // mutable let name = "Ada"; let pi: f64 = 3.14159; if age >= 18 { println!("{} is an adult", name); } for i in 0..5 { println!("Count: {}", i); }}
Ownership & Borrowing
Rust's compile-time memory safety model.
rust
fn main() { let s1 = String::from("hello"); let s2 = s1; // s1 moved into s2, s1 no longer valid // println!("{}", s1); // compile error: value moved let s3 = String::from("world"); let len = calculate_length(&s3); // borrow, not move println!("{} has length {}", s3, len);}fn calculate_length(s: &String) -> usize { s.len()}
Error Handling
Result, Option, and the ? operator.
rust
use std::fs::File;fn read_file(path: &str) -> Result<String, std::io::Error> { let contents = std::fs::read_to_string(path)?; // ? propagates errors Ok(contents)}fn main() { match read_file("data.txt") { Ok(text) => println!("{}", text), Err(e) => println!("Error: {}", e), }}
Core Keywords
Common Rust language keywords and types.
- let/let mut- immutable and mutable variable bindings
- &/&mut- shared and exclusive (mutable) borrows
- match- exhaustive pattern matching on values/enums
- Option<T>- Some(value) or None instead of null
- Result<T, E>- Ok(value) or Err(error) for fallible operations
- impl- implements methods or traits on a type
- trait- defines shared behavior, similar to an interface
- lifetime ('a)- annotation ensuring references remain valid
Pro Tip
Prefer iterator combinators (.map(), .filter(), .fold()) over manual loops with indices — they're idiomatic, zero-cost, and avoid bounds-check bugs.
Was this cheat sheet helpful?
Explore Topics
#Rust#RustCheatSheet#Programming#Advanced#BasicSyntax#OwnershipBorrowing#ErrorHandling#CoreKeywords#CheatSheet#SkillVeris
Advertisement
Sri Hayavadhana Info-Tech
Professional Web Designing Services
- Responsive Websites
- E-commerce Solutions
- SEO Friendly Design
- Fast & Secure
- Support & Maintenance