diff --git a/Examples.md b/Examples.md index 5214bb7..74a55f4 100644 --- a/Examples.md +++ b/Examples.md @@ -1079,4 +1079,58 @@ fn main let another = x: i64 -> i64 --> x*x // but not too bad I think ``` -Of course, there's no real reason to implement that until/unless we actually want anonymous regular (non-closure) functions. \ No newline at end of file +Of course, there's no real reason to implement that until/unless we actually want anonymous regular (non-closure) functions. + +##### Capturing + + + + + + + +
RustHusht
+ +``` +fn main() { + use std::mem; + + let consume = || { + println!("`movable`: {:?}", movable); + mem::drop(movable); + }; + + // `consume` consumes the variable so this can only be called once. + consume(); + + let haystack = vec![1, 2, 3]; + + let contains = move |needle| haystack.contains(needle); + + println!("{}", contains(&1)); + println!("{}", contains(&4)); +} +``` + + +``` +fn main + use std::mem + + let consume = => + println! "`movable`: {:?}", movable + mem::drop movable + + // `consume` consumes the variable so this can only be called once. + consume() + + + let haystack = vec![1, 2, 3] + + let contains = move needle => haystack.contains needle + + println! "{}", contains(&1) //! That we need the & is Rust weirdoness + println! "{}", contains(&4) + +``` +