main.rs
1 fn main() { 2 let s1 = gives_ownership(); // gives_ownership moves its return 3 // value into s1 4 5 let s2 = String::from("hello"); // s2 comes into scope 6 7 let s3 = takes_and_gives_back(s2); // s2 is moved into 8 // takes_and_gives_back, which also 9 // moves its return value into s3 10 11 println!("{s1} {s3}"); 12 } // Here, s3 goes out of scope and is dropped. s2 was moved, so nothing 13 // happens. s1 goes out of scope and is dropped. 14 15 fn gives_ownership() -> String { // gives_ownership will move its 16 // return value into the function 17 // that calls it 18 19 let some_string = String::from("yours"); // some_string comes into scope 20 21 some_string // some_string is returned and 22 // moves out to the calling 23 // function 24 } 25 26 // This function takes a String and returns one 27 fn takes_and_gives_back(a_string: String) -> String { // a_string comes into 28 // scope 29 30 a_string // a_string is returned and moves out to the calling function 31 }