Skip to content

Instantly share code, notes, and snippets.

@justanotherdot
Last active August 4, 2020 23:32
Show Gist options
  • Save justanotherdot/3f5df86c094b1630924d3f23a292450b to your computer and use it in GitHub Desktop.
Save justanotherdot/3f5df86c094b1630924d3f23a292450b to your computer and use it in GitHub Desktop.
Utilizing closures as snapshots for single or multiple values.
fn main() {
let mut xs = vec![1];
xs[0] = 42;
let ys = xs.clone();
let snapshot = move || ys;
xs[0] = 100;
let reset = snapshot();
assert_eq!(&reset, &vec![42]);
assert_eq!(xs, vec![100]);
xs = reset;
assert_eq!(xs, vec![42]);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment