RUST: &str methods

//! Comprehensive examples of &str methods in Rust

pub fn demonstrate_str_methods() {
    let example_str = " Hello, Rust! ";
    let substring = "Rust";
    
    // 1. Basic Operations
    // Get length in bytes (not chars)
    let length = example_str.len();
    println!("Length: {}", length); // 14 (includes spaces)
    
    // Check if empty
    let empty_check = example_str.is_empty();
    println!("Is empty: {}", empty_check); // false
    
    // Iterate over Unicode chars
    println!("Chars:");
    for c in example_str.chars() {
        print!("{} ", c);
    }
    println!();
    
    // Iterate over raw bytes
    println!("Bytes:");
    for b in example_str.bytes() {
        print!("{} ", b);
    }
    println!();
    
    // 2. Searching
    // Check if contains substring
    let contains = example_str.contains(substring);
    println!("Contains 'Rust': {}", contains); // true
    
    // Check prefix
    let starts_with = example_str.starts_with(" Hello");
    println!("Starts with ' Hello': {}", starts_with); // true
    
    // Check suffix
    let ends_with = example_str.ends_with("! ");
    println!("Ends with '! ': {}", ends_with); // true
    
    // Find first occurrence (returns Option<usize>)
    let find_pos = example_str.find('R');
    println!("'R' at position: {:?}", find_pos); // Some(8)
    
    // Find last occurrence
    let rfind_pos = example_str.rfind('!');
    println!("Last '!' at position: {:?}", rfind_pos); // Some(12)
    
    // 3. Slicing
    // Safe substring access (returns Option<&str>)
    let sub = example_str.get(0..5);
    println!("Substring 0..5: {:?}", sub); // Some(" Hell")
    
    // Split by delimiter
    println!("Split by comma:");
    for part in example_str.split(',') {
        println!("'{}'", part.trim());
    }
    
    // Split by whitespace
    println!("Split by whitespace:");
    for word in example_str.split_whitespace() {
        println!("'{}'", word);
    }
    
    // Split into two at index
    let (left, right) = example_str.split_at(7);
    println!("Split at 7: '{}' | '{}'", left, right);
    
    // 4. Transformation
    // Case conversion (returns new String)
    let lower = example_str.to_lowercase();
    println!("Lowercase: '{}'", lower);
    
    let upper = example_str.to_uppercase();
    println!("Uppercase: '{}'", upper);
    
    // Trimming whitespace
    let trimmed = example_str.trim();
    println!("Trimmed: '{}'", trimmed);
    
    let trim_start = example_str.trim_start();
    println!("Trim start: '{}'", trim_start);
    
    let trim_end = example_str.trim_end();
    println!("Trim end: '{}'", trim_end);
    
    // 5. Comparison
    // String equality
    let equal = example_str.eq(" Hello, Rust! ");
    println!("Equal to ' Hello, Rust! ': {}", equal); // true
    
    // Lexicographical comparison
    let cmp = example_str.cmp("Hello");
    println!("Comparison result: {:?}", cmp); // Ordering::Greater
}

// Remember to add this module to src/menu_items/mod.rs
// with: pub mod string_method_examples;

"I'm sorry that your stepmother is a nympho"