2 releases

Uses old Rust 2015

0.1.1 Oct 26, 2017
0.1.0 Oct 26, 2017

#15 in #fibonacci-sequence

MPL-2.0 license

8KB
72 lines

fibonacci

A Fibonacci generator written in Rust. It's generic for types that implement num::Zero, num::One, num::CheckedAdd, and Clone.

There is no stipulation that the type be num::Unsigned, but the generator will never generate a negative number.

The generator handles overflow by returning None, stopping the iterator. For example, Fibonacci<u64> will overflow after the ninety-second element, so the iterator will end after producing its ninety-second element.

The following all hold true:

assert_eq!(12, Fibonacci::<u8>::default().take(300).collect::<Vec<_>>().len());
assert_eq!(23, Fibonacci::<u16>::default().take(300).collect::<Vec<_>>().len());
assert_eq!(46, Fibonacci::<u32>::default().take(300).collect::<Vec<_>>().len());
assert_eq!(92, Fibonacci::<u64>::default().take(300).collect::<Vec<_>>().len());

Note that u128 will not work, as the num crate does not implement any traits for it.


lib.rs:

Generate Fibonacci sequence numbers.

// Collect all of the Fibonacci numbers that fit inside a u8.
let some_numbers: Vec<u8> = Fibonacci::default().collect();
assert_eq!(some_numbers, vec![1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233]);

Dependencies

~240KB