Created
September 16, 2015 23:20
-
-
Save t2ru/35b7eac7bbd24b472387 to your computer and use it in GitHub Desktop.
generic fibonacci in rust
This file contains hidden or 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
extern crate num; | |
//use std::num::One; | |
use num::traits::One; | |
use std::cmp::PartialOrd; | |
use std::ops::{Add, Sub}; | |
fn fib<T>(n: T) -> T where | |
T: One + PartialOrd + Add<Output=T> + Sub<Output=T> + Clone { | |
if n < T::one() + T::one() { | |
n | |
} else { | |
fib(n.clone() - T::one() - T::one()) + fib(n.clone() - T::one()) | |
} | |
} | |
pub fn main() { | |
println!("Hello, world!"); | |
println!("{}", fib(35)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment