๐Ÿฆ€/100 Projects/Notes/Source

src/main.rs

View on GitHub
use std::rc::Rc;
use std::cell::RefCell;
 
fn main() {
    println!("๐Ÿง  Memory Management Demo in Rust");
 
    // Ownership
    let s1 = String::from("Ownership Example");
    let s2 = s1; // s1 moved
    println!("๐Ÿ”‘ Ownership transferred: {}", s2);
    // println!("{}", s1); // โŒ Error: s1 moved
 
    // Borrowing
    let s3 = String::from("Borrowing Example");
    borrow_demo(&s3);
    println!("โœ… After borrow: {}", s3); // still accessible
 
    // Mutable Borrowing
    let mut s4 = String::from("Hello");
    mutate_demo(&mut s4);
    println!("๐Ÿ”ง After mutation: {}", s4);
 
    // Lifetimes
    let result;
    let a = String::from("abcd");
    {
        let b = String::from("xyz");
        result = longest(&a, &b);
        println!("โณ Longest string: {}", result);
    }
 
    // Box (heap allocation)
    let boxed = Box::new(42);
    println!("๐Ÿ“ฆ Boxed value: {}", boxed);
 
    // Rc (reference-counted pointer)
    let rc_val = Rc::new(String::from("Shared"));
    let rc_clone = Rc::clone(&rc_val);
    println!("๐Ÿ“š Rc values: {}, {}", rc_val, rc_clone);
    println!("Ref count: {}", Rc::strong_count(&rc_val));
 
    // RefCell (interior mutability)
    let cell = RefCell::new(100);
    *cell.borrow_mut() += 50;
    println!("๐Ÿงช RefCell value: {}", cell.borrow());
}
 
fn borrow_demo(data: &String) {
    println!("๐Ÿ“ฅ Borrowed: {}", data);
}
 
fn mutate_demo(data: &mut String) {
    data.push_str(" World");
}
 
fn longest<'a>(x: &'a str, y: &'a str) -> &'a str {
    if x.len() > y.len() { x } else { y }
}

โ† Back to folder