Skip to content

Instantly share code, notes, and snippets.

@FrozenDroid
Last active November 5, 2018 00:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save FrozenDroid/684b6471ca56dae48fb19da0355b2ef6 to your computer and use it in GitHub Desktop.
Save FrozenDroid/684b6471ca56dae48fb19da0355b2ef6 to your computer and use it in GitHub Desktop.
#![feature(try_trait)]
// Fibonacci sequence takes last 2 numbers and sums them, but you can change as you like :)
const TAKE_COUNT: usize = 2;
fn main() {
let mut vec: Vec<i64> = Vec::with_capacity(100);
loop {
let last = vec.iter().rev().take(TAKE_COUNT).collect::<Vec<_>>();
match last
.get(0..TAKE_COUNT)
.or(Some(&[&1]))
.and_then(|a| a.iter().try_fold(0 as i64, |acc, &x| acc.checked_add(*x)))
{
Some(add) => vec.push(add),
_ => break, // If the checked_add overflowed
}
}
println!("{:?}", vec);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment