Day-5: Coding exercises for the last four-day learning.
Introduction:
To solidify your understanding of Rust's basic concepts, it's crucial to practice writing code and solving coding exercises. In this article, we'll provide you with a set of coding exercises focused on variable creation, the classic "Hello, World!" example, loops, and control flow. These exercises will help you gain hands-on experience and reinforce your knowledge of Rust fundamentals. Let's dive in!
Exercise 1:
Variable Creation Create a program that prompts the user to enter their name and age. Store the values in variables and display them on the console.
use std::io;
fn main() {
let mut name = String::new();
let mut age = String::new();
println!("Enter your name: ");
io::stdin().read_line(&mut name).expect("Failed to read input.");
println!("Enter your age: ");
io::stdin().read_line(&mut age).expect("Failed to read input.");
println!("Name: {}", name.trim());
println!("Age: {}", age.trim());
}
Exercise 2:
Hello, World! Write a program that prints "Hello, World!" to the console.
fn main() {
println!("Hello, World!");
}
Exercise 3:
Loop and Control Flow Write a program that prints the numbers from 1 to 10, but excludes the number 7. Use loop and control flow statements to achieve this.
fn main() {
for number in 1..=10 {
if number != 7 {
println!("{}", number);
}
}
}
Exercise 4:
Odd or Even Write a program that prompts the user to enter a number. Determine if the number is odd or even and display the result on the console.
use std::io;
fn main() {
let mut number = String::new();
println!("Enter a number: ");
io::stdin().read_line(&mut number).expect("Failed to read input.");
let number: i32 = number.trim().parse().expect("Invalid input.");
if number % 2 == 0 {
println!("Even");
} else {
println!("Odd");
}
}
Exercise 5:
A sum of Numbers Writes a program that calculates and displays the sum of all numbers from 1 to 100. Use a loop to iterate through the numbers and a variable to keep track of the sum.
fn main() {
let mut sum = 0;
for number in 1..=100 {
sum += number;
}
println!("Sum: {}", sum);
}
Exercise 6:
Fibonacci Sequence Write a program that generates the Fibonacci sequence up to a specified number of terms. Prompt the user to enter the number of terms they want to generate and display the sequence on the console.
use std::io;
fn main() {
let mut terms = String::new();
println!("Enter the number of terms: ");
io::stdin().read_line(&mut terms).expect("Failed to read input.");
let terms: u32 = terms.trim().parse().expect("Invalid input.");
let mut num1 = 0;
let mut num2 = 1;
let mut count = 0;
println!("Fibonacci Sequence:");
while count < terms {
println!("{}", num1);
let next = num1 + num2;
num1 = num2;
num2 = next;
count += 1;
}
}
Exercise 7:
Number Guessing Game Create a number guessing game where the program generates a random number between 1 and 100. Prompt the user to guess the number and provide feedback (e.g., too high, too low) until they guess correctly. Track the number of attempts and display it when the user guesses correctly.
use std::io;
use rand::Rng;
fn main() {
let secret_number = rand::thread_rng().gen_range(1..=100);
let mut attempts = 0;
println!("Welcome to the Number Guessing Game!");
loop {
let mut guess = String::new();
println!("Enter your guess (1-100): ");
io::stdin().read_line(&mut guess).expect("Failed to read input.");
let guess: u32 = match guess.trim().parse() {
Ok(num) => num,
Err(_) => {
println!("Invalid input. Please enter a valid number.");
continue;
}
};
attempts += 1;
match guess.cmp(&secret_number) {
std::cmp::Ordering::Less => println!("Too low!"),
std::cmp::Ordering::Greater => println!("Too high!"),
std::cmp::Ordering::Equal => {
println!("Congratulations! You guessed the number in {} attempts.", attempts);
break;
}
}
}
}
Conclusion:
Coding exercises are an excellent way to reinforce your understanding and practical skills in Rust programming. In this article, we provided a set of exercises covering variable creation, the classic "Hello, World!" example, loops, and control flow. Practice these exercises to gain confidence in utilizing variables, loops, and control flow mechanisms in your Rust programs. Remember to analyze the problems, break them down into smaller steps, and leverage your understanding of Rust's syntax and concepts. Happy coding and continue exploring the vast possibilities of Rust!
I hope this helps, you!!
More such articles:
https://www.youtube.com/@maheshwarligade
\==========================**=========================
If this article adds any value to you then please clap and comment.
Let’s connect on Stackoverflow, LinkedIn, & Twitter.