rustbook-learning-guide 0.1.0

A comprehensive Rust learning guide with practical examples covering ownership, traits, polymorphism, and more
Documentation
//! # Ownership and Borrowing
//! 
//! This module demonstrates Rust's ownership system, borrowing rules, and memory management.
//! Understanding these concepts is crucial for writing safe and efficient Rust code.

/// Demonstrates basic ownership concepts
pub fn ownership_examples() {
    println!("\n🔒 Ownership Examples");
    println!("{}", "-".repeat(20));
    
    // Basic ownership
    let s1 = String::from("hello");
    let s2 = s1; // s1 is moved to s2
    // println!("{}", s1); // This would cause a compile error
    println!("s2: {}", s2);
    
    // Clone to avoid move
    let s3 = String::from("world");
    let s4 = s3.clone();
    println!("s3: {}, s4: {}", s3, s4);
    
    // Function ownership
    let s = String::from("hello");
    takes_ownership(s);
    // println!("{}", s); // This would cause a compile error
    
    let x = 5;
    makes_copy(x);
    println!("x is still valid: {}", x); // This works because i32 implements Copy
}

fn takes_ownership(some_string: String) {
    println!("Taking ownership of: {}", some_string);
} // some_string goes out of scope and is dropped

fn makes_copy(some_integer: i32) {
    println!("Making copy of: {}", some_integer);
} // some_integer goes out of scope, but nothing special happens

/// Demonstrates borrowing and references
pub fn borrowing_examples() {
    println!("\n📚 Borrowing Examples");
    println!("{}", "-".repeat(20));
    
    let s1 = String::from("hello");
    let len = calculate_length(&s1);
    println!("The length of '{}' is {}.", s1, len);
    
    // Mutable references
    let mut s = String::from("hello");
    change(&mut s);
    println!("Changed string: {}", s);
    
    // Multiple immutable references are allowed
    let r1 = &s;
    let r2 = &s;
    println!("r1: {}, r2: {}", r1, r2);
    
    // But only one mutable reference at a time
    let r3 = &mut s;
    println!("r3: {}", r3);
}

fn calculate_length(s: &String) -> usize {
    s.len()
} // s goes out of scope, but because it doesn't have ownership, nothing happens

fn change(some_string: &mut String) {
    some_string.push_str(", world");
}

pub fn slice_examples() {
    println!("\n🍰 Slice Examples");
    println!("{}", "-".repeat(20));
    
    let s = String::from("hello world");
    
    // String slices
    let hello = &s[0..5];
    let world = &s[6..11];
    let whole = &s[..]; // Entire string
    
    println!("hello: {}, world: {}, whole: {}", hello, world, whole);
    
    // Array slices
    let a = [1, 2, 3, 4, 5];
    let slice = &a[1..3];
    println!("Array slice: {:?}", slice);
    
    // First word function
    let word = first_word(&s);
    println!("First word: {}", word);
}

fn first_word(s: &str) -> &str {
    let bytes = s.as_bytes();
    
    for (i, &item) in bytes.iter().enumerate() {
        if item == b' ' {
            return &s[0..i];
        }
    }
    
    &s[..]
}