---
title: "Write a Rust program to calculate the Fibonacci sequence up to a given number of terms."  
description: "Write a Rust program to calculate the Fibonacci sequence up to a given number of terms."  
author: "Utpal Vishwas"  
published: 2023-06-22  
updated: 2023-06-23  
canonical: https://www.mindstick.com/forum/158827/write-a-rust-program-to-calculate-the-fibonacci-sequence-up-to-a-given-number-of-terms  
category: "rust"  
tags: ["programs", "rust"]  
reading_time: 2 minutes  

---

# Write a Rust program to calculate the Fibonacci sequence up to a given number of terms.

Write a [Rust](https://www.mindstick.com/forum/160170/explain-rust-s-ownership-borrowing-and-lifetimes-system) [program to calculate](https://www.mindstick.com/forum/158772/write-a-python-program-to-calculate-the-sum-of-digits-in-a-given-number) the [Fibonacci sequence](https://www.mindstick.com/interview/34368/fibonacci-sequence-up-to-n-terms) up to a given number of terms.

## Replies

### Reply by Aryan Kumar

Sure, here is a [Rust program](https://www.mindstick.com/forum/158800/create-a-rust-program-to-find-the-gcd-greatest-common-divisor-of-two-numbers) to calculate the [Fibonacci](https://www.mindstick.com/forum/34692/how-to-generate-fibonacci-triangle-in-c-sharp) [sequence](https://www.mindstick.com/interview/34504/difference-between-identity-vs-sequence) up to a given number of terms:

Rust

```plaintext
fn fibonacci(n: i32) -> Vec<i32> {
    let mut sequence = vec![0, 1];
    for i in 2..n + 1 {
        let next_fibonacci_number = sequence[i - 1] + sequence[i - 2];
        sequence.push(next_fibonacci_number);
    }
    return sequence;
}

fn main() {
    let n = 10;
    let fibonacci_sequence = fibonacci(n);
    println!("The Fibonacci sequence up to {} terms is:", n);
    for number in fibonacci_sequence {
        println!("{}", number);
    }
}
```

This program first defines a function called `fibonacci` that takes an i32 number as input and returns a Vec of i32. The function first initializes a vector called `sequence` to the first two Fibonacci numbers, 0 and 1. Then, the function iterates through the numbers from 2 to n + 1. For each number, the function calculates the next Fibonacci number by adding the previous two Fibonacci numbers. Finally, the function returns the `sequence` vector.

The main function of the program then calls the `fibonacci` function with the value 10 as input. The result of the function call is then printed to the console.

To run the program, you can save it as a file called `fibonacci.rs` and then compile it using the following command:

Code snippet

```plaintext
rustc fibonacci.rs
```

Once the program is compiled, you can run it using the following command:

Code snippet

```plaintext
./fibonacci
```

This will print the following output to the console:

Code snippet

```plaintext
The Fibonacci sequence up to 10 terms is:
0
1
1
2
3
5
8
13
21
34
```


---

Original Source: https://www.mindstick.com/forum/158827/write-a-rust-program-to-calculate-the-fibonacci-sequence-up-to-a-given-number-of-terms

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
