Created
May 8, 2017 12:41
-
-
Save king6cong/48ddf9efb40ab9e54aba2194976352bc to your computer and use it in GitHub Desktop.
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 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