Does passing a String to a function by value move its pointer, length, and capacity without copying the heap data, just like let s2 = s1?
The Rust Programming Language Forum
Does passing a String to a function by value move its pointer, length, and capacity without copying the heap data, just like let s2 = s1?
fn main() { let s1 = String::from("ganesha"); // The String's metadata (pointer, length, and capacity) // is moved to s2; the actual heap data ("ganesha") is not copied. // so both will point same and single heap data. // let s2 = s1; //My question is: does the same thing happen when passing s1 this way to func take_ownership(s1); // println!("{s1}"); // Error: s1 was moved } fn take_ownership(s2: String) { println!("{s2}"); }
0 comments
No comments yet.