Skip to content

Instantly share code, notes, and snippets.

@king6cong
Created May 8, 2017 12:41
Show Gist options
  • Save king6cong/48ddf9efb40ab9e54aba2194976352bc to your computer and use it in GitHub Desktop.
Save king6cong/48ddf9efb40ab9e54aba2194976352bc to your computer and use it in GitHub Desktop.
fn test_closure_recursive() {
struct Fact<'s> {
f: &'s Fn(&Fact, u32) -> u32,
}
impl<'s> Fact<'s> {
pub fn call(&self, i: u32) -> u32 {
(self.f)(&self, i)
}
}
let fact = Fact { f: &|fact, x| if x == 0 { 1 } else { x * (fact.f)(fact, x - 1) } };
// println!("{}", (fact.f)(&fact, 5));
println!("{}", fact.call(5));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment