Skip to content

Instantly share code, notes, and snippets.

@Geal
Created January 14, 2015 12:47
Show Gist options
  • Save Geal/540ddd745045e7335c0d to your computer and use it in GitHub Desktop.
Save Geal/540ddd745045e7335c0d to your computer and use it in GitHub Desktop.
create a closure depending on a parameter
fn prod<'a,'b>(a:uint) -> Box<FnMut(&'b[u8]) -> &'b[u8] +'a> {
box move |&: input: &'b[u8]| -> &'b[u8] {
input.slice_from(a)
}
}
fn main() {
let mut a = prod(2);
println!("a1: {}", a("abcdefgh".as_bytes()));
// -> a1: [99, 100, 101, 102, 103, 104]
println!("a2: {}", a("ghij".as_bytes()));
// a2: [105, 106]
let mut b = prod(3);
println!("b1: {}", b("abcdefgh".as_bytes()));
// -> a1: [100, 101, 102, 103, 104]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment