1. SETUP:
# Install Rust
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
# Check version
rustc --version
# Create new project
cargo new my_project
cd my_project
# Build and run
cargo build
cargo run
2. BASICS:
fn main() {
println!("Hello, world!"); // Print
let x = 5; // Immutable variable
let mut y = 10; // Mutable variable
const PI: f64 = 3.1415; // Constant
}
3. DATA TYPES:
// Scalar
let a: i32 = -42;
let b: u32 = 42;
let c: f64 = 3.14;
let d: bool = true;
let e: char = 'R';
// Compound
let tup: (i32, f64, char) = (500, 6.4, 'z');
let (x, y, z) = tup; // Destructuring
let arr = [1, 2, 3, 4, 5];
let slice: &[i32] = &arr[1..3];
4. CONTROL FLOW:
if x > 5 {
println!("Greater!");
} else {
println!("Smaller!");
}
for n in 1..5 { // 1 to 4
println!("{}", n);
}
let mut counter = 0;
while counter < 3 {
println!("count = {}", counter);
counter += 1;
}
5. FUNCTIONS:
fn add(a: i32, b: i32) -> i32 {
a + b // last expression = return
}
fn main() {
println!("{}", add(5, 3));
}
6. OWNERSHIP & BORROWING:
fn main() {
let s = String::from("Hello");
takes_ownership(s); // moved
// println!("{}", s); // ❌ cannot use after move
let x = 5;
makes_copy(x); // i32 implements Copy
println!("{}", x); // ✅ still valid
let s2 = String::from("Hi");
borrow(&s2); // pass by reference
println!("{}", s2); // ✅ still valid
}
fn takes_ownership(s: String) {
println!("{}", s);
}
fn makes_copy(x: i32) {
println!("{}", x);
}
fn borrow(s: &String) {
println!("{}", s);
}
7. STRUCTS & ENUMS:
struct User {
name: String,
age: u8,
}
enum Direction {
Up,
Down,
Left,
Right,
}
fn main() {
let user = User { name: String::from("Alice"), age: 25 };
println!("{} is {}", user.name, user.age);
let dir = Direction::Left;
match dir {
Direction::Up => println!("Up"),
Direction::Down => println!("Down"),
Direction::Left => println!("Left"),
Direction::Right => println!("Right"),
}
}
8. TRAITS & IMPL:
trait Greet {
fn say_hello(&self);
}
struct Person {
name: String,
}
impl Greet for Person {
fn say_hello(&self) {
println!("Hello, {}!", self.name);
}
}
fn main() {
let p = Person { name: String::from("Bob") };
p.say_hello();
}
9. ERROR HANDLING:
fn divide(a: f64, b: f64) -> Result<f64, String> {
if b == 0.0 {
Err(String::from("Division by zero!"))
} else {
Ok(a / b)
}
}
fn main() {
match divide(10.0, 2.0) {
Ok(res) => println!("Result: {}", res),
Err(err) => println!("Error: {}", err),
}
}
10. COLLECTIONS:
// Vector
let mut v = vec![1, 2, 3];
v.push(4);
for i in &v {
println!("{}", i);
}
// HashMap
use std::collections::HashMap;
let mut scores = HashMap::new();
scores.insert("Alice", 10);
scores.insert("Bob", 20);
println!("{:?}", scores);
11. COMMON MACROS:
println!("Hello {}!", "Rust"); // Print with formatting
dbg!(42); // Debug print to stderr
vec![1, 2, 3]; // Create vector
12. LIFETIMES (BASIC):
fn longest<'a>(a: &'a str, b: &'a str) -> &'a str {
if a.len() > b.len() { a } else { b }
}
fn main() {
let x = String::from("abcd");
let y = "xyz";
println!("{}", longest(&x, y));
}
13. CARGO COMMANDS:
cargo new project_name # Create new project
cargo build # Build project
cargo run # Run project
cargo check # Check for errors
cargo test # Run tests
cargo fmt # Format code
cargo doc --open # Generate docs
Top comments (0)