I've just finished reading " What Is Ownership?" section of the book.
I remember writing a C swap function to swap two integers that involves using a temp variable and parameters having to be pointers.
Now I want to use Rust to swap two different strings. (That seems impossible in C)
Here is the code.
fn main() { let s1 = String::from("Hello"); let s2 = String::from("World"); println!("{} {}", s1, s2); let (s1, s2) = swap(s1, s2); println!("{} {}", s1, s2); } fn swap(s1: String, s2: String) -> (String, String) { (s2, s1) }
After compiling and running, I get what I want to see happen.
But the code I write makes me feel weird as no temp variable is used and the s1 s2 which are assigned the second time are different from the previous s1 and s2 .
I think that's not actually swapped.
Any better ideas to write swap strings?