I am struggling to find a way to take two values from the end of a vector, sum those values, and push the sum to the vector.
I have found that pop
, truncate
, and drain
do not work because they remove the values from the original vector.
fn main() {
println!("Which Fibonacci number would you like to find?");
let mut fib_num = String::new();
io::stdin().read_line(&mut fib_num)
.expect("Failed to read line");
let fib_num: u32 = fib_num.trim().parse()
.expect("Please enter a number");
let mut stored_nums: Vec<u32> = vec![0, 1];
while fib_num > stored_nums.len() as u32 {
let mut limit = stored_nums.len();
let mut new_num1 = stored_nums.pop().unwrap();
let mut new_num2 = stored_nums.pop().unwrap_or(0);
stored_nums.push(new_num1 + new_num2);
}
}