 # Exercises - Rust Fundamentals

Post your solution to Rust Fundamentals here.

Remember to encapsulate your code following this guide: FAQ - How to post code in the forum

1 Like Convert temperatures between Fahrenheit and Celsius

Notice that I used `:1` for the output conversion result to round the output decimal to 1.
If you require further accuracy, you can replace `:1` with `:n` where n represents the number of decimal points available for the result.

``````    let celsius1 = 37.0;
let celsius2 = 0.0;
let farenheit1 = 32.0;
let farenheit2 = 104.0;

// Celsius => Farenheit: (°C × 1.8) + 32 = °F
println!("{celsius} C = {farenheit:.1} F", celsius = celsius1, farenheit = (celsius1 * 1.8) + 32.0); // Prints 37 C = 98.6 F
println!("{celsius} C = {farenheit:.1} F", celsius = celsius2, farenheit = (celsius2 * 1.8) + 32.0); // Prints 0 C = 32.0 F

// Farenheit => Celsius: (°F − 32) × 5.0 / 9.0 = °C
println!("{farenheit} F = {celsius:.1} C", farenheit = farenheit1, celsius = (farenheit1 - 32.0) * 5.0 / 9.0); // Prints 32 F = 0.0 C
println!("{farenheit} F = {celsius:.1} C", farenheit = farenheit2, celsius = (farenheit2 - 32.0) * 5.0 / 9.0); // Prints 104 F = 40.0 C
``````

Output

``````37 C = 98.6 F
0 C = 32.0 F
32 F = 0.0 C
104 F = 40.0 C
`````` Generate the nth Fibonacci number.

Perhaps there’s a much better solution than this.
Having the n’th number represent a 0 index was fairly easy, however having `n` represent the n’th number (index + 1 was slightly harder.

``````    let n = 10;     // This is our input
let mut result = 0;
let mut n_minus_1 = 0;
let mut n_minus_2 = 0;

for i in 0..=n {
if i == n {
println!("The {}th Fibonacci number is {}", n, result);
break;
}
if i == 1 {
result = 1;
} else if i == 2 {
n_minus_1 = result;
n_minus_2 = 1;
} else {
result = n_minus_1 + n_minus_2;
n_minus_2 = n_minus_1;
n_minus_1 = result;
}
}
``````

Output

``````The 10th Fibonacci number is 34
`````` Print the lyrics to the Christmas carol “The Twelve Days of Christmas,” taking advantage of the repetition in the song.

``````    let mut day;
let song: [&str; 6] = ["And a partridge in a pear tree.", "Two turtle doves,", "Three French hens,", "Four calling birds,", "Five golden rings,", "Six geese a-laying,"];
let mut j;

for i in 1..=6 {
match i {
1 => { day = "First"; }
2 => { day = "Second"; }
3 => { day = "Third"; }
4 => { day = "Fourth"; }
5 => { day = "Fifth"; }
_ => { day = "Sixth"; }
}
println!("On the {} day of Christmas", day);
println!("My true love gave to me");

if i == 1 {
println!("A partridge in a pear tree.");
} else {
j = i;

loop {
println!("{}", song[j - 1]);
j = j - 1;
if j < 1 {
break;
}
}
}
println!("");
}
``````

Output

``````On the First day of Christmas
My true love gave to me
A partridge in a pear tree.

On the Second day of Christmas
My true love gave to me
Two turtle doves,
And a partridge in a pear tree.

On the Third day of Christmas
My true love gave to me
Three French hens,
Two turtle doves,
And a partridge in a pear tree.

On the Fourth day of Christmas
My true love gave to me
Four calling birds,
Three French hens,
Two turtle doves,
And a partridge in a pear tree.

On the Fifth day of Christmas
My true love gave to me
Five golden rings,
Four calling birds,
Three French hens,
Two turtle doves,
And a partridge in a pear tree.

On the Sixth day of Christmas
My true love gave to me
Six geese a-laying,
Five golden rings,
Four calling birds,
Three French hens,
Two turtle doves,
And a partridge in a pear tree.
``````
1 Like
``````fn main() {

// 1. Convert temperatures between Fahrenheit and Celsius.
let celsius1 = 37.0;
let celsius2 = 0.0;
let fahrenheit1 = 32.0;
let fahrenheit2 = 104.0;
let f1 = celsius1 * 1.8 + 32.0;
let f2 = celsius2 * 1.8 + 32.0;
let c1 =(fahrenheit1 - 32.0) /1.8;
let c2 = (fahrenheit2 - 32.0) /1.8;
println!("{} C = {} F", celsius1, f1);
println!("{} C = {} F", celsius2, f2);
println!("{} C = {} F", c1, fahrenheit1);
println!("{} C = {} F", c2, fahrenheit2);

// 2. Generate the nth Fibonacci number.
let n = 15;
let mut x = [0; 15];
x = 1;
let mut i = 2;
while i <= 14{
x[i] = x[i-1] + x[i-2];
i = i + 1;
}

println!("{}th Fibonacci number: {}", n, x[n-1]);

// 3. Print the lyrics to the Christmas carol
// “The Twelve Days of Christmas,”
// taking advantage of the repetition in the song.

let days = ["first", "second", "third", "fourth", "fifth", "sixth"];
let two = "Two turtle-doves";
let three = "Three French hens";
let four = "Four calling birds";
let five = "Five golden rings (five golden rings)";
let six = "Six geese a laying";
let numbers = [two, three, four, five, six];
let mut i = 1;
while i <= 6 {
println!{"On the {} day of Christmas", days[i-1] };
println!{"My true love sent to me" };
for j in (2..=i).rev(){
println!("{}", numbers[j-2]);
}

if i == 1 {
print!("A ")
} else {
print!("And a ")
}

println!{"partridge in a pear tree" };
println!{"" };
i = i + 1;
}

}
``````
1 Like Exercise Spoiler Alert

Code
``````fn main() {
// 1. Convert temperatures between Fahrenheit and Celsius.
fn temperatures(_temp_type: &str, _temp_value: u16) {
match _temp_type {
"F" => {
let _to_celsius: u16 = (_temp_value - 32) * 5 / 9;
println!("Fahrenheit:{} => Celsius:{}", _temp_value, _to_celsius);
}
"C" => {
// 15 °C = 15 × 9/5 + 32 = 59 °F
let _to_fahrenheit: u16 = _temp_value * 9 / 5 + 32;
println!("Celsius:{} =>  Fahrenheit:{}", _temp_value, _to_fahrenheit);
}
_ => {
println!("Incorrect temperature type");
}
}
}

// 2. Generate the nth Fibonacci number.
fn fibonacci(n: u16) {
let mut num1: u16 = 0;
let mut num2: u16 = 1;
let mut sum: u16;
let mut i: u16 = 0;
loop {
sum = num1 + num2;
num1 = num2;
num2 = sum;
println!("fibonnaci: {}", sum); // statements inside the loop
i = i + 1; // increment
if i > n {
// terminal condition
break;
}
}
}

// 3. Print the lyrics to the Christmas carol
// “The Twelve Days of Christmas,”
// taking advantage of the repetition in the song.
fn christmas_carol() {
let ordinal = [
"first", "second", "third", "fourth", "Fifth", "Sixth", "Seventh", "Eighth", "Ninth",
"Tenth", "Eleventh", "Twelfth",
];
let verses = [
"A partridge in a pear tree",
"Two turtle doves, and",
"Three french hens",
"Four calling birds",
"Five golden rings",
"Six geese a-laying",
"Seven swans a-swimming",
"Eight maids a-milking",
"Ten lords a-leaping",
"Eleven pipers piping",
"Twelve drummers drumming",
];

for i in 0..ordinal.len() {
println!(
"On the {} day of Christmas, my true love sent to me",
ordinal[i]
);
if i == 0 {
println!("{}", verses);
} else {
let mut j = i;
loop {
println!("{}", verses[j]);
if j == 0 {
break;
}
j = j - 1;
}
}
}
}

temperatures("F", 50);
temperatures("C", 10);
fibonacci(10);
christmas_carol();
}

``````
Exercises
``````fn main() {

// 1. Convert temperatures between Fahrenheit and Celcius

let celsius1 = 37.0;
let celsius2 = 0.0;
let farenheit1 = 32.0;
let farenheit2 = 104.0;

println!("{} C is {} F", celsius1, (celsius1 * 9./5.) + 32.);
println!("{} C is {} F", celsius2, (celsius2 * 9./5.) + 32.);
println!("{} F is {} C", farenheit1, (farenheit1-32.) * 5./9.);
println!("{} F is {} C", farenheit2, (farenheit2-32.) * 5./9.);

// 2. Generate the nth Fibonacci number
let n = 600;
let mut i = 1;
let mut prev = i;

while i <= n {
let temp = prev;
println!("{}", i + prev);
prev = i;
i = i + temp;
}

// 3. Print the lyrics to the Christmas carol

let nth = ["first", "second", "third", "fourth", "fifth", "sixth"];

let lyrics = [
"Six geese a-laying",
"Five golden rings",
"Four calling birds",
"Three french hens",
"Two turtle doves, and",
"A partridge in a pear tree"
];

let mut i = 0;
while i < lyrics.len() {
println!("On the {} day of Christmas, my true love sent to me", nth[i]);
for n in 0..=i {
println!("{}", lyrics[i - n]);
}
println!("");
i = i + 1;
}

}
``````
1 Like
Exercises- Rust Fundamentals , my code:
1. Convert temperatures between Fahrenheit and Celsius.
``````    let celsius1 = 37.0;
let celsius2 = 0.0;
let farenheit1 = 32.0;
let farenheit2 = 104.0;

// Celsius -> Farenheit (C * 1.8) + 32
println!("{} C = {:.1} F", celsius1, (celsius1 * 1.8) + 32.0);
println!("{} C = {:.1} F", celsius2, (celsius2 * 1.8) + 32.0);

// Farenheit -> Celsius (F - 32) * 5 / 9
println!("{:.1} F = {} C", farenheit1, (farenheit1 - 32.0) * 5.0 / 9.0);
println!("{:.1} F = {} C", farenheit2, (farenheit2 - 32.0) * 5.0 / 9.0);
``````
1. Generate the nth Fibonacci number.
``````    // Fibonacci Sequence formula: Fib_n = Fib_n-1 + Fib_n-2
// Fib_0 = 0, Fib_1 = 1, Fib_2 = 1

let n = 15;
let mut prev = 0;
let mut curr = 1;
let _i = 1;

for _i in 2..=n {
let res = prev + curr;
prev = curr;
curr = res;
}

println!("\nThe {}th Fibonacci number: {}\n", n, curr);
``````
1. Print the lyrics to the Christmas carol
``````    // “The Twelve Days of Christmas,”
// taking advantage of the repetition in the song.
let days = ["first", "second", "third", "fourth", "fifth", "sixth"];
let lyrics = [
"And a partridge in a pear tree\n",
"Two turtledoves",
"Three French hens",
"Four calling birds",
"Five gold rings (five golden rings)",
"Six geese a-laying",
];

for i in 0..days.len() {
println!("On the {} day of Christmas,", days[i]);
println!("my true love sent to me");

if i == 0 {
println!("A partridge in a pear tree\n");
} else {
let mut j = i;
loop {
println!("{}", lyrics[j]);
if j == 0 {
break;
}
j -= 1;
}
}
}
``````
1 Like

Hey ! I had to do some research to get this exercise done , it was really fun. I think it looks good ``````fn main() {
// 1. Convert temperatures between Fahrenheit and Celsius.
let mut celsius1 = 37.0;
let mut celsius2 = 0.0;
let mut farenheit1 = 32.0;
let mut farenheit2 = 104.0;

let temperatures = [celsius1 , celsius2 , farenheit1 , farenheit2];

for i in 0..2{
fromCelsiusToFahrenheit(temperatures[i]);
}
for i in 2..4{
fromFahrenheitToCelsius(temperatures[i]);
}

// 2. Generate the nth Fibonacci number.
let n = 15 ; //get the (n)th number of Fibonacci sequence
reachFibonacciNumber(15);
// 3. Print the lyrics to the Christmas carol

sing();

}

//----------------------------------------------------------------------------------

fn fromCelsiusToFahrenheit(mut celsius : f32){
println!("{initial} C = {convertion} F" , initial = celsius,  convertion = celsius * 9.00 / 5.00 + 32.00);

}
fn fromFahrenheitToCelsius(mut fahrenheit : f32){
println!("{initial} F = {convertion} C" , initial = fahrenheit, convertion = (fahrenheit -32.00) * 5.00 / 9.00 );

}

fn reachFibonacciNumber(mut target_number : i32){
let target  = target_number;
let mut  n = 1;
let mut previousN = 0;
let mut counter = 1;

loop{
let y = n ;
n = n+previousN;
previousN = y;
counter = counter + 1;
if(counter == target ){
println!("The 15th Fibonacci number is : {}" , n);
break;
}

}

}

fn sing(){

const days : [&str; 7]  = ["" , "first" , "second" , "third" , " fourth", "fifth" , "sixth" ];
let mut currentDay = 1;
let mut  what_he_sent = "A partridge in a pear tree".to_string();
const  gift_list : [&str ; 5] = ["Two turtle-doves \n" , "Three French hens \n" ,  "Four calling birds \n" , "Five golden rings (five golden rings) \n" , "Six geese a laying \n"];

loop{

println!("On the {nthDay} day of Christmas \n My true love sent to me \n  {list} \n\n  "
, nthDay = days[currentDay] ,list =  what_he_sent);

let new_verse = gift_list[currentDay -1 ];
what_he_sent = format!("{} {}", new_verse, what_he_sent);
currentDay = currentDay + 1;

if currentDay > 6 {
break;
}

}
}

``````
``````fn main() {
// 1. Convert temperatures between Fahrenheit and Celsius.
let celsius1 = 37.0;
let celsius2 = 0.0;
let farenheit1 = 32.0;
let farenheit2 = 104.0;

println!("{} C is {} F", celsius1, celsius1 * 9.0 / 5.0 + 32.0);
println!("{} C is {} F", celsius2, celsius2 * 9.0 / 5.0 + 32.0);
println!("{} F is {} C", farenheit1, (farenheit1 - 32.0) * 5.0 / 9.0);
println!("{} F is {} C", farenheit2, (farenheit2 - 32.0) * 5.0 / 9.0);
println!();

// 2. Generate the nth Fibonacci number.
let mut f = [0; 15];

f = 1;
for i in 2..f.len() {
f[i] = f[i-2] + f[i-1];
}
for i in 0..f.len() {
println!("{:2}. {:4}", i+1, f[i]);
}

// 3. Print the lyrics to the Christmas carol
// “The Twelve Days of Christmas,”
// taking advantage of the repetition in the song.
let carol = [
("first", "a partridge in a pear tree"),
("second", "two turtle-doves"),
("third", "three french hens"),
("fourth", "four calling birds"),
("fifth", "five golden rings"),
("sixth", "six geese a laying"),
];
for i in 0..carol.len() {
println!();
println!("On the {} day of Christmas", carol[i].0);
println!("My true love sent to me");
for n in (0..i+1).rev() {
if n == 0 && i > 0 { print!("And ") }
println!("{}", carol[n].1);
}
}
}
``````
1 Like
`````` // 1. Convert temperatures between Fahrenheit and Celsius.
//  let celsius1 = 37.0;
//  let celsius2 = 0.0;
//  let farenheit1 = 32.0;
//  let farenheit2 = 104.0;

//  println!("0.0 C = 32.0 F");

let celsius1 = 37.0;
let celsius2 = 0.0;
let farenheit1 = 32.0;
let farenheit2 = 104.0;

println!("{} C = {:.1} F", celsius1, (celsius1 * 1.8) + 32.0);
println!("{} C = {:.1} F", celsius2, (celsius2 * 1.8) + 32.0);

println!("{:.1} F = {} C", farenheit1, (farenheit1 - 32.0) * 5.0 / 9.0);
println!("{:.1} F = {} C", farenheit2, (farenheit2 - 32.0) * 5.0 / 9.0);
println!("======================== \n");

// 2. Generate the nth Fibonacci number.
let n = 15;
//solution
let mut x = [0; 15];
x = 1;
let mut i = 2;
while i <= 14{
x[i] = x[i-1] + x[i-2];
i = i + 1;
}
println!("nth(n={}) Fibonacci number: {}", n, x[n-1]);
println!("======================== \n");

// 3. Print the lyrics to the Christmas carol
// “The Twelve Days of Christmas,”
// taking advantage of the repetition in the song.
let nth = ["first", "second", "third", "fourth", "fifth", "sixth"];

let lyrics = [
"Six geese a-laying",
"Five golden rings",
"Four calling birds",
"Three french hens",
"Two turtle doves, and",
"A partridge in a pear tree"
];

let mut i = 0;
while i < lyrics.len() {
println!("On the {} day of Christmas, \n my true love sent to me.", nth[i]);
for n in 0..=i {
println!("{}", lyrics[i - n]);
}
println!("");
i = i + 1;
}
println!("========================");
``````

Output

``````Answer I :
37 C = 98.6 F
0 C = 32.0 F
32.0 F = 0 C
104.0 F = 40 C
========================

nth(n=15) Fibonacci number: 377
========================

On the first day of Christmas,
my true love sent to me.
Six geese a-laying

On the second day of Christmas,
my true love sent to me.
Five golden rings
Six geese a-laying

On the third day of Christmas,
my true love sent to me.
Four calling birds
Five golden rings
Six geese a-laying

On the fourth day of Christmas,
my true love sent to me.
Three french hens
Four calling birds
Five golden rings
Six geese a-laying

On the fifth day of Christmas,
my true love sent to me.
Two turtle doves, and
Three french hens
Four calling birds
Five golden rings
Six geese a-laying

On the sixth day of Christmas,
my true love sent to me.
A partridge in a pear tree
Two turtle doves, and
Three french hens
Four calling birds
Five golden rings
Six geese a-laying

========================
``````
``````pub fn run() {
// 1. Convert temperatures between Fahrenheit and Celsius.
let celsius1 = 37.0;
let celsius2 = 0.0;
let farenheit1 = 32.0;
let farenheit2 = 104.0;

println!("0.0 C = 32.0 F");

fn get_fahr(celc_degree: f64) -> f64 {
return celc_degree * 1.8000000 + 32.0;
}
fn get_cels(fahr_degree: f64) -> f64 {
return fahr_degree - 32.0 / 1.8000000;
}

println!("{} C = {} F", celsius1, get_fahr(celsius1));
println!("{} C = {} F", celsius2, get_fahr(celsius2));
println!("{} C = {} F", farenheit1, get_cels(farenheit1));
println!("{} C = {} F", farenheit2, get_cels(farenheit2));

// 2. Generate the nth Fibonacci number.
let n = 15;

fn get_nth(n: u64) -> u64 {
let mut n1: u64 = 0;
let mut n2: u64 = 1;
let mut next: u64;

for _1 in 1..=n {
next = n1 + n2;
n1 = n2;
n2 = next;
// print!("{} ", n1);
}

return n1;
}

print!("{}nth value of Fib.seq. = {}", n, get_nth(n));

// 3. Print the lyrics to the Christmas carol
// Half of the “The Twelve Days of Christmas,”
// taking advantage of the repetition in the song.

let days = ["first", "second", "third", "fourth", "fifth", "sixth"];
"A partridge in a pear tree",
"Two turtle-doves",
"Three French hens",
"Four calling birds",
"Five golden rings",
"Six geese a laying",
];

print!("

The {} Days of Christmas

On the {} day of Christmas
My true love sent to me
{}
);

for _i in 1..=days.len()-1 {

print!("
On the {} day of Christmas
My true love sent to me
", days[_i]
);

let mut gix = _i;
while gix >= 1 {
print!(
"{}
);
gix = gix - 1;
}

print!(
"And a partridge in a pear tree
");
}
}

``````
``````fn main() {

// 1. Convert temperatures between Fahrenheit and Celsius.

println!("\nExercise 1\n");

let celsius1 = 37.0;

let celsius2 = 0.0;

let farenheit1 = 32.0;

let farenheit2 = 104.0;

let cels_temperatures = [celsius1, celsius2];

let fahr_temperatures = [farenheit1, farenheit2];

fn get_fahr(celc_degree: f64) -> f64 {

return celc_degree*1.8 + 32.0;

}

fn get_cels(fahr_degree: f64) -> f64 {

return (fahr_degree - 32.0)/1.8;

}

for temp in cels_temperatures{

println!("{} C = {:.1} F", temp, get_fahr(temp));

}

for temp in fahr_temperatures{

println!("{} F = {:.1} C", temp, get_cels(temp));

}

// 2. Generate the nth Fibonacci number.

println!("\nExercise 2\n");

fn fibonacci(n: u32) -> u32 {

match n {

0 => 0,

1 => 1,

_ => fibonacci(n - 1) + fibonacci(n - 2),

}

}

let n = 15;

println!("The {}th number in the Fibonacci sequence is {}.", n, fibonacci(n));

// 3. Print the lyrics to the Christmas carol “The Twelve Days of Christmas,” taking advantage of the repetition in the song.

println!("\nExercise 3\n");

fn day(n: u8) -> &'static str{

match n {

1 => "first",

2 => "second",

3 => "third",

4 => "fourth",

5 => "fifth",

6 => "sixth",

_ => "invalid",

}

}

fn line(n: u8) -> &'static str{

match n {

1 => "And a partridge in a pear tree",

2 => "Two turtle-doves",

3 => "Three French hens",

4 => "Four calling birds",

5 => "Five golden rings (five golden rings)",

6 => "Six geese a laying",

_ => "invalid",

}

}

for i in 1..=6{

println!("On the {} day of Christmas", day(i));

println!("My true love sent to me");

for j in (1..=i).rev() {

println!("{}", line(j));

}

println!(" ");
}
}
``````