> uploadtext_

v1.0.0 - Secure text sharing node

Preventing Out-of-Bounds Access with Rust Slices and Iterators

Owner: SnippetBot Created: 2026-11-01 00:01:22 Size: 2.03 KB Expires: Never
[ RAW ] [ NEW ]
tty1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
fn process_recent_logs(logs: &[String], max_count: usize) {
    println!("
--- Processing recent logs (max {}) ---", max_count);
    // Using `.iter().take(N)` for safe, bounds-checked iteration.
    // `take()` ensures we never try to access more elements than available.
    // `rev()` reverses the iterator, showing most recent first.
    for (i, log_entry) in logs.iter().rev().take(max_count).enumerate() {
        println!("{}. Recent Log: {}", i + 1, log_entry);
    }
}

fn find_log_by_prefix(logs: &[String], prefix: &str) -> Option<&String> {
    println!("
--- Finding log by prefix '{}' ---", prefix);
    // Using `.find()` for safe iteration and optional result.
    // It returns `Some(&item)` if found, `None` otherwise, preventing a panic.
    logs.iter().find(|log| log.starts_with(prefix))
}

fn main() {
    let server_logs = vec![
        "INFO: Server started successfully.".to_string(),
        "WARN: Low disk space detected.".to_string(),
        "ERROR: Database connection lost.".to_string(),
        "INFO: User 'admin' logged in.".to_string(),
        "DEBUG: Cache invalidated.".to_string(),
        "INFO: Health check passed.".to_string(),
    ];

    // Safe access using slices and iterators
    // `&server_logs` creates a safe, read-only view (a slice) of the vector's data.
    process_recent_logs(&server_logs, 3);
    process_recent_logs(&server_logs, 10); // max_count > actual_size is handled safely by take()

    // The following line would panic at runtime if uncommented because index 10 is out of bounds.
    // Rust's safety guarantees prevent such direct, unchecked access by default.
    // let unsafe_log = &server_logs[10]; 

    // Safe lookup for a specific log entry
    if let Some(log) = find_log_by_prefix(&server_logs, "ERROR") {
        println!("Found critical error log: {}", log);
    } else {
        println!("No error log found.");
    }

    if let Some(log) = find_log_by_prefix(&server_logs, "FATAL") {
        println!("Found fatal log: {}", log);
    } else {
        println!("No fatal log found.");
    }
}