Skip to content

Instantly share code, notes, and snippets.

@rust-play
Created August 22, 2019 08:02
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 rust-play/823535d2ce51a319e3988cb0f5de681e to your computer and use it in GitHub Desktop.
Save rust-play/823535d2ce51a319e3988cb0f5de681e to your computer and use it in GitHub Desktop.
Code shared from the Rust Playground
fn main() {
let s = String::from("hello"); // s comes into scope
takes_ownership(s); // s's value moves into the function...
// ... and so is no longer valid here
let x = 5; // x comes into scope
makes_copy(x); // x would move into the function,
// but i32 is Copy, so it’s okay to still
// use x afterward
} // Here, x goes out of scope, then s. But because s's value was moved, nothing
// special happens.
fn takes_ownership(some_string: String) { // some_string comes into scope
println!("{}", some_string);
} // Here, some_string goes out of scope and `drop` is called. The backing
// memory is freed.
fn makes_copy(some_integer: i32) { // some_integer comes into scope
println!("{}", some_integer);
} // Here, some_integer goes out of scope. Nothing special happens.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment