Register Login

Generating the Fibonacci sequence up to 'n' numbers in Rust

Updated May 10, 2024

In this article, we will be generating the Fibonacci sequence up to ‘n’ numbers using the Rust programming language.

This article assumes you know the common fundamentals of Rust, including how to use the cargo package manager, basic macros such as println!, and the fundamentals of variables and while loops.

If you are not aware of these, I’d highly recommend checking out chapter 3 of the Rust book.

Common Programming Concepts - The Rust Programming Language

What is the Fibonacci sequence?

In mathematics, the Fibonacci sequence is a sequence in which each number is the sum of the two preceding ones. Numbers that are part of the Fibonacci sequence are known as Fibonacci numbers, commonly denoted Fn .

The sequence commonly starts from 0 and 1. Starting from 0 and 1, the sequence begins:

0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144,

The Fibonacci numbers may be defined by the recurrence relation

F (0) = 0 , F(1) = 1 ,

and

F (n) = F (n − 1) + F (n − 2)

for n > 1.

Setting up the Rust environment:

Create a new directory for this project and run the following command:

cargo new fibonacciseq

Then enter the directory using:

cd fibonacciseq

This command will generate a main.rs file in src/main.rs with the following code:

fn main() {
	println!("Hello World!");
}

We can run this code using:

cargo run

Output:

Code logic:

In order to perform this task, we are going to follow the following approach

  1. Get user input for the value of n.
  2. Create 3 variables a, b, and c that will perform the same function as F(n), F(n-1), and F(n-2)
  3. Create a while loop that repeats the addition of the above variables, from 0 to n.
  4. Print all the values of an after each calculation.

Full Code:

// Import standard library
use std::io;

// Define variables in the main function
fn main() {
	let mut n = String::new();
	
	// Define variable count = 1
	let mut count:u32 = 1;

	// Define variable b = 0
	let mut b:u32 = 0;

	// Define variable c = 1
	let mut c:u32 = 1;

	// Define variable a = unassigned
	let mut a:u32 ;

	println!("This program generates the Fibonacci sequence upto 'n' numbers.");

	// Input from user to generate n Fibonacci sequence
	io::stdin()
	.read_line(&mut n)
	.expect("failed to read");

	// Convert string to unsigned 32 bit integer 
	let n:u32 = n.trim().parse().expect("wrong");
	
	// Print the first number
	println!("0");

	// Now use the while loop to print the remaining numbers
	while n > count{
    	a = b + c;
    	c = b;
    	b = a;
    	count += 1;
		println!("{a}");
	}
}

Step 1: Defining Variables:

Inside your main.rs file, replace the println!(“Hello World!”) macro with the following:

println!("This program generates the fibonacci sequence upto 'n' numbers.");

Then define the following variables, which we will be using later:

let mut n = String::new();  // This is our input n
let mut count:u32 = 1; // This is the count variable that we will use in our while loop.
let mut b:u32 = 0;  //  This is the b variable, which will be F(0) at start, and later, F(n-2).
let mut c:u32 = 1; // This is the c variable, which will be F(1) at start, and later F(n-1).
let mut a:u32;  // This is a variable, which is F(n).

We have defined all of these variables to be mutable so that we can control their value later on.

We are using the u32 data type, which is a 32-bit unsigned integer, meaning that it will always have a positive value.

However, the n variable is a string, as in Rust, in order to take a number as input, you will have to receive it as a string, and later parse it as a number.

Step 2: Receiving user input:

Underneath the print macro, add the following snippet:

io::stdin()
.read_line(&mut n)
.expect("failed to read")

This will take input from the user, assign it to the variable we defined earlier, called ‘n’, and raise an exception if it is unable to read it.

In order to receive input, however, we will have to import the io module from the std crate.

Add the following code to the top of your script:

use std::io;

Now we can take input from the user.

Step 3: Parsing the input string as an int:

This will be a short step, basically what we will do here is that we will enter the following line of code, which will parse our input string.

let n:u32 = n.trim().parse().expect("wrong");

Put this line below the line where we received the user input using io::stdin()

Here, we are converting the n variable, which we defined earlier from the string datatype to a 32-bit unsigned integer.

trim() removes the newline from the string, parse() converts it to an integer, and .expect() raises an error if the string is non-parseable.

Step 4: Calculating the sequence:

Now that everything is set up, we will finally start generating the sequence.

Create a while loop, after the line where the variable is parsed into an int.

while n > count {
	count += 1;
}

As you can see, we are using n and count as conditions for the loop. Once n = c, the loop will be terminated.

We have chosen such a condition as it will calculate the values we want, until the nth number.

Now according to the equation that we saw earlier, let us assign new values to the variables we have defined inside the while loop:

while n > count{
	a = b + c;
    c = b;
    b = a;
	println!(“{a}”)
    count += 1;
}

Now let us understand what is going on here.

If the user enters, for example, 9, then the code will do the following:

At count = 1:
a = b + c, where b = 0 and c = 1
Thus, a  = 1
Then we assign b to c and a to b:
Thus,  b = 1, and c = 0

Print: 1

At count = 2:
a =  b + c
-> a = 1 + 0
c = b =1 and b = a =1

Print = 1

At count = 3:
a = b + c
-> a = 1 + 1 = 2
and c = b = 1, b = a =2

Print: 2

And so on until count = 9

Running our code now, we get:

The code seems fine, but there is one problem:

Only 8 numbers from the sequence are generated, even though we entered 9. Also, 0 is missing from the generated numbers.

This is actually because we have assigned the default value of the count to be 1. This is because if we choose the count to be 0, then an extra number will be generated.

The reason is that there is no way to calculate 0 here.

To fix this problem, all we have to do is add the following macro right above the while loop:

println!("0");

And now if we run our code:

We can see the desired behavior.

Bonus Step: Generating only the nth number:

If we wanted to change our code to make it generate only the nth Fibonacci number, we can do the following steps:

1: Remove the following line which we added in the last step:

println!(“0”);

2: Assigning a default value of 0 to the variable a:

let mut a:u32 = 0;

If we do not do this, we will get an error.

3: And finally, moving the line that prints the numbers outside the while loop:

​​while n > count{
     a = b + c;
     c = b;
     b = a;
     count += 1;
}
println!("{a}");

Your final code should look like this:

use std::io;

fn main() {
	let mut n = String::new();
	let mut count:u32 = 1;
	let mut b:u32 = 0;
	let mut c:u32 = 1;
	let mut a:u32 = 0;

	println!("This program generates the fibonacci sequence upto 'n' numbers.");
	io::stdin()
	.read_line(&mut n)
	.expect("failed to read");

	let n:u32 = n.trim().parse().expect("wrong");

	//println!("0");
	while n > count{
		a = b + c;
	    c = b;
	    b = a;
	    count += 1;
     }
	println!("{a}");
}

And now if we run our code, we get the desired output:

Conclusion:

We explored some basic fundamentals of Rust in this article and used them to generate the Fibonacci sequence.

It is recommended to check out the Rust book for understanding the fundamentals.

After you are done with this project, you could also try making a calculator cli and a temperature converter that converts temperature from fahrenheit to celsius.


×