DeveloperBreeze

Rust Programming Tutorials, Guides & Best Practices

Explore 4+ expertly crafted rust tutorials, components, and code examples. Stay productive and build faster with proven implementation strategies and design patterns from DeveloperBreeze.

Rust Cheatsheet

Cheatsheet August 29, 2024
rust

struct Color(i32, i32, i32);
let black = Color(0, 0, 0);
enum Message {
    Quit,
    Move { x: i32, y: i32 },
    Write(String),
    ChangeColor(i32, i32, i32),
}

Advanced Memory Management in Rust: Understanding Ownership, Borrowing, and Lifetimes

Tutorial August 27, 2024
rust

fn main() {
    let s = String::from("hello");
    let len = calculate_length(&s); // borrow s immutably
    println!("The length of '{}' is {}.", s, len); // s is still valid
}

fn calculate_length(s: &String) -> usize {
    s.len()
}
  • Immutable references (&T) allow read-only access.
  • Mutable references (&mut T) allow read-write access but are exclusive.
  • You cannot have mutable and immutable references to the same data simultaneously.