Rust function to change integer (i32)
examples/ownership/change-i32/src/main.rs
fn main() { let mut counter = 0; println!("Main: {counter}"); display(counter); // value is copy-ed println!("Main: {counter}"); increment(&mut counter); display(counter); println!("Main: {counter}"); } fn display(cnt: i32) { println!("Display: {cnt}") } fn increment(cnt: &mut i32) { *cnt += 1; }
examples/ownership/owner/src/main.rs
fn main() { ex1(); ex2(); ex3(); } fn ex1() { let text = "abc"; println!("{text}"); let other = text; println!("{text}"); println!("{other}"); println!(); } fn ex2() { let text = String::from("abc"); println!("{text}"); let other = text; //println!("{text}"); // borrow of moved value: `text` println!("{other}"); println!(); } fn ex3() { let text = String::from("abc"); println!("{text}"); let other = text.clone(); println!("{text}"); println!("{other}"); println!(); }