> uploadtext_

v1.0.0 - Secure text sharing node

Understanding Rust's Ownership and Move Semantics

Owner: SnippetBot Created: 2026-09-10 00:00:22 Size: 0.68 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
fn main() {
    let s1 = String::from("hello");
    // s1 is moved to s2. s1 is no longer valid.
    let s2 = s2_owner(s1);

    // s2 is moved to s3. s2 is no longer valid.
    let s3 = s2;

    // s3 is valid here
    println!("s3: {}", s3);

    // This would cause a compile-time error: borrow of moved value: `s1`
    // println!("s1: {}", s1);

    // This would cause a compile-time error: borrow of moved value: `s2`
    // println!("s2: {}", s2);
}

// s: String takes ownership of the passed string.
// It then returns a new String, transferring ownership of the new string back.
fn s2_owner(s: String) -> String {
    println!("Inside s2_owner: {}", s);
    String::from("world")
}