Safe Passage

Volume 0: Simulation Core | Time to complete: 10 mins

To graduate to the live production server, you must pass data through the simulation without destroying it. You are about to meet Rust's strictest security mechanism: Ownership.

1. The Law of Ownership

If you give a string of data to a function in Rust, that function consumes and destroys it to keep memory clean. Once moved, the original variable is gone forever. If you want a function to simply "look" at the data without destroying it, you must lend it a Read-Only Reference using the & symbol.

Think of it like a library. You don't give the book away; you let the function read it, and when the function is done, you retain ownership.

fn scan_log(log: &String) {
    println!("Scanning reference: {}", log);
}

fn main() {
    let dummy_log = String::from("Network spike detected.");
    
    // We pass a reference (&) so 'scan_log' doesn't destroy the data
    scan_log(&dummy_log);
    
    // We can still safely use the data here!
    println!("Log intact on main thread: {}", dummy_log);
}