Skip to content

Instantly share code, notes, and snippets.

@rust-play
Created November 13, 2019 20:37
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/cea0754c666a71783b01ddd99ede57c4 to your computer and use it in GitHub Desktop.
Save rust-play/cea0754c666a71783b01ddd99ede57c4 to your computer and use it in GitHub Desktop.
Code shared from the Rust Playground
// 1. Run as-is.
// 2. Comment in the section marked (2). Read the compiler error.
// 3. Comment in the section marked (3). Look how output differs from (1).
use std::mem;
fn print_size_of<T>(_t: &T) {
println!("size: {}", mem::size_of::<T>());
}
fn main() {
let print_xs;
{
let xs = vec![1, 2, 3, 4];
print_xs = /* (3) move */ || {
for (i, x) in xs.iter().enumerate() {
println!("{}: {}", i, x);
}
};
print_size_of(&print_xs);
}
/* (2) print_xs(); */
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment