Last active
November 5, 2018 00:47
-
-
Save FrozenDroid/684b6471ca56dae48fb19da0355b2ef6 to your computer and use it in GitHub Desktop.
This file contains 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
#![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