diff options
author | Daniel Thompson <daniel.thompson@linaro.org> | 2022-11-02 17:16:41 +0000 |
---|---|---|
committer | Daniel Thompson <daniel.thompson@linaro.org> | 2022-11-02 17:16:41 +0000 |
commit | 50989bd29dc5f340a274a61488164c65617a931f (patch) | |
tree | aa68fbe8f1a832a53aeb861d2046a6279dedea43 | |
parent | 8ee8cb711bdc36996aa7c146324242c2d8bb0e14 (diff) |
Make Fib generic and test with u64
-rw-r--r-- | Cargo.toml | 3 | ||||
-rw-r--r-- | src/main.rs | 24 |
2 files changed, 18 insertions, 9 deletions
@@ -4,3 +4,6 @@ version = "0.1.0" edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +num = "0.4.0" diff --git a/src/main.rs b/src/main.rs index 30a9c29..2f270cd 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,17 +1,23 @@ +use num::{One, Zero}; +use std::ops::Add; + #[derive(Debug)] -pub struct Fib { - a: u32, - b: u32, +pub struct Fib<T> { + a: T, + b: T, } -impl Fib { +impl<T: Zero + One> Fib<T> { pub fn new() -> Self { - Self { a: 0, b: 1 } + Self { + a: T::zero(), + b: T::one(), + } } } -impl Iterator for Fib { - type Item = u32; +impl<T: Copy + Add<Output = T>> Iterator for Fib<T> { + type Item = T; fn next(&mut self) -> Option<Self::Item> { let res = self.a; @@ -23,8 +29,8 @@ impl Iterator for Fib { } fn main() { - // more than 46 and we'll get an overflow - for (i, f) in (0..46).zip(Fib::new()) { + // more than 92 and we'll get an overflow + for (i, f) in (0..92).zip(Fib::<u64>::new()) { println!("{i:4}: {f:24}"); } } |