Ownership
We have already show that passing or returning a Vector or String would result also in passing the ownership of its memory.
Lets observe in the following example this interaction:
- In rust there are not function prototypes, so the order where the functions are written doesn’t matter.
- Pay attention to the ownership of the String.
- it is extremely clear what is the in and out of the function.
- runtime impact is minimum since the memory is moved instead of copied.
fn main(){ let mut a: String = String::from("TA"); a.push_str("C"); println!("{:?}", a); let b = myfunc(a); println!("{:?}", b); } fn myfunc(mut arg1: String) -> String { println!("{:?}", arg1); arg1.push_str("OS"); arg1 }