// a bunch of little examples of ownership in rust // ggtowell Oct 2023 fn main() { main3() } fn main1() { let aa = "hello".to_string(); receiver(aa); //println!("{aa}"); // Does not compile, the call to receiver gave up ownership of aa } // becomes owner of aa fn receiver(aa: String) { //aa.push_str("gg"); println!("{aa}"); } // after each line in main3, what variables are printable (if any) and what is printed?? fn main3() { let s1 = 5; { println!("A s1 {s1}"); let s1 = g_o(); println!("B s1 {s1}"); let s2 = String::from("hello"); println!("C s2 {s2} s1 {s1}"); let s3 = t_a_g(s2); println!("D s3 {s3} s1 {s1}"); } println!("E s1 {s1}"); } fn g_o() -> String { let some_string = String::from("yours"); some_string } fn t_a_g(a_string: String) -> String { a_string } // one way of handling ownership is to return the things // that the function became an owner of // this is a poor example because i32 is passed by value (passed by copy??) fn main4() { let (a, b) = r2s2(4,5); } fn r2s2(aa:i32, bb:i32) -> (i32, i32) { return (aa+5, bb+7) }