January 2019
Beginner to intermediate
554 pages
13h 31m
English
We'll need a program to debug to experience gdb. Let's create a new project by running cargo new buggie. Our program will have a single function, fibonacci, which takes a position of n as usize, and gives us the nth Fibonacci number. This function assumes that the initial values of the Fibonacci numbers are 0 and 1. The following code is the program in its entirety:
1 // buggie/src/main.rs2 3 use std::env;4 5 pub fn fibonacci(n: u32) -> u32 {6 let mut a = 0;7 let mut b = 1;8 let mut c = 0;9 for _ in 2..n {10 let c = a + b;11 a = b;12 b = c;13 }14 c15 }16 17 fn main() {18 let arg = env::args().skip(1).next().unwrap();19 let pos = str::parse::<u32>(&arg).unwrap();20 let nth_fib = fibonacci(pos);21 println!("Fibonacci ...Read now
Unlock full access