Advertisement
cwchen

[Rust] Recursion demo

Aug 23rd, 2017
383
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 0.27 KB | None | 0 0
  1. fn fib(n: u32) -> u32 {
  2.     if n == 0 { // Base condition 1
  3.         0
  4.     } else if n == 1 { // Base condition 2
  5.         1
  6.     } else {
  7.         fib(n - 1) + fib(n - 2)
  8.     }
  9. }
  10.  
  11. fn main() {
  12.     for i in 0..11 {
  13.         print!("{} ", fib(i));
  14.     }
  15.     println!("");
  16. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement