Help with the fibonacci sequence problem given at the end of chapter 3

⚓ Rust    📅 2025-08-17    👤 surdeus    👁️ 7      

surdeus

Hello people.
A beginner here, super beginner actually (I dont have any coding experience and I started with this language only), and I was doing the exercise given at the end of chapter 3. The fibonacci sequence thing. And I am very confused. I dont understand how to proceed after this code I have written. I think it should use a 'for' loop and that it should break and return value when the value reaches the desired number, but I dont have the direction on how to integrate the fibonacci formula f(n) = f(n-1) + f(n-2). I didnt wanna consult an AI agent cause it would give me the whole answer and not just a hint. Plus i have heard much of this community on how welcoming it is so i just wanted to introduce myself too. Here is the code. Note that I used match cause i saw it in a code and understood how to use it (I am following the book and not jumping lessons). Plus the use of I/O standard library is from the example provided in chapter three itself.

use std::io;
fn the_number_getter(prompt: &str) -> u32 {
    loop {
        println!("{}", prompt);
        let mut n = String::new();
        io::stdin()
            .read_line(&mut n)
            .expect("Cannot read the writing");

        match n.trim().parse::<u32>() {
            Ok(num) => return num,
            Err(_) => println!("AGAIN!!!"),
        }
    }
}

fn main() {
    let first = 0;
    let second = 1;
    let the_number = the_number_getter("Enter the number bruh:");
    let the_array = [0..];
    
    for elements in the_array {
        let n = the_array[];
        
    } 
}

I would appreciate any hints and help. Feel free to ask me any questions. Plus tell me if i am going in the right direction.
Thank you for going through this. (It was unbearable listening to my rant, I know hehehe)

1 post - 1 participant

Read full topic

🏷️ Rust_feed