Skip to content

Instantly share code, notes, and snippets.

@kscz
Last active December 25, 2019 23:11
Show Gist options
  • Select an option

  • Save kscz/0a56dc848dc2b0e0ecf9efe8331b7f8b to your computer and use it in GitHub Desktop.

Select an option

Save kscz/0a56dc848dc2b0e0ecf9efe8331b7f8b to your computer and use it in GitHub Desktop.
fn main() {
let x = String::from("Mysterious first letter!");
let ref_x = first_letter(&x);
take_ownership(x); // ERROR: ref_x still exists and is "borrowing" x
// We can't delete x safely!
println!("{}ystery solved!", ref_x);
}
fn first_letter(input: &str) -> &str {
&input[0..1]
}
fn take_ownership(of_this: String) -> () {
println!("I will destroy you: \"{}\"", of_this);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment