Created
January 2, 2019 00:57
-
-
Save pianomanfrazier/417c3898609bb3ddae104fac17e30ace to your computer and use it in GitHub Desktop.
Playing around with Rust ownership
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fn main() { | |
let x = String::from("hello"); // allocate from the heap | |
let mut y = x; | |
// let mut y = x.clone(); // clone if you want to use x again | |
y.push_str(", world! "); | |
append(&mut y); | |
print(&y); | |
append(&mut y); | |
print(&y); | |
// print(&x); // breaks because no Copy trait | |
} | |
fn print(s: &String) { | |
println!("{}", s); | |
} | |
fn append(s: &mut String) { | |
s.push_str("--append"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment