我正在努力寻找一种方法,从一个向量的末端取两个值,求和这些值,然后把和推到向量上.
我发现pop
、truncate
和drain
不起作用,因为它们从原始向量中移除了值.
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);
}
}