Skip to content

Instantly share code, notes, and snippets.

@pianomanfrazier
Created January 2, 2019 00:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pianomanfrazier/417c3898609bb3ddae104fac17e30ace to your computer and use it in GitHub Desktop.
Save pianomanfrazier/417c3898609bb3ddae104fac17e30ace to your computer and use it in GitHub Desktop.
Playing around with Rust ownership
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