Last active
August 4, 2020 23:32
-
-
Save justanotherdot/3f5df86c094b1630924d3f23a292450b to your computer and use it in GitHub Desktop.
Utilizing closures as snapshots for single or multiple values.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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