Skip to content

Instantly share code, notes, and snippets.

@joelreymont
Forked from anonymous/playground.rs
Created September 5, 2016 16:16
Show Gist options
  • Save joelreymont/249c7e1245cbed24299d8be1859e1f81 to your computer and use it in GitHub Desktop.
Save joelreymont/249c7e1245cbed24299d8be1859e1f81 to your computer and use it in GitHub Desktop.
Initialize with closures
pub type uint = u64;
pub fn initialize_with_closure<F>(rules: F) -> uint where F: FnOnce(&mut uint) {
let mut i = 0;
rules(&mut i);
i
}
// equivalently
pub fn initialize_with_closure_explicit<F>(rules: F) -> uint where F: for<'a> FnOnce(&'a mut uint) -> () {
let mut i = 0;
rules(&mut i);
i
}
pub fn main() {
initialize_with_closure(|i: &mut uint| *i = *i + 20);
initialize_with_closure_explicit(|i: &mut uint| *i = *i + 20);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment