Rust Beginners Exercises with Solutions and Explanations
Rust Basic Exercises [19 exercises with solution and Explanation]
[An editor is available at the bottom of the page to write and execute the scripts. Go to the editor]
1. Write a Rust program that gathers system information such as the Rust version, operating system details, and system architecture.
2. Write a Rust program that accepts two numbers from the user, adds them together, and displays the result.
3. Write a Rust program that declares a mutable variable counter and initializes it with 0. Then increase it by 1 and decrease it by 1. At the end, print the variable value for each stage.
4. Write a Rust program that converts an integer to a string and vice versa and prints the result.
5. Write a Rust program that performs basic Math operations - addition, subtraction, multiplication, and division operations on two integers.
6. Write a Rust program that checks if a number is even or odd and prints the result.
7. Write a Rust program that counts from 1 to 10 using a loop and prints each number.
8. Write a Rust program that prints all even numbers from 1 to 20 using a while loop.
9. Write a Rust program that prints all elements of an array using a for loop.
10. Write a Rust program that defines a function that calculates the factorial of a given number and returns the result.
11. Write a Rust program that creates two variables p and q, assigns a value to p, then assigns p to q and try to use p again.
12. Write a Rust function that takes a reference to a variable as a parameter and modifies its value.
Click me to see the solution13. Write a Rust function that takes two string references and returns the smallest one.
14. Write a Rust program that defines a struct Person with fields like name and age.
15. Write a Rust program that creates an instance of the Person struct and prints its fields.
Click me to see the solution16. Write a Rust program that defines an enum Color with variants representing different colors.
17. Write a Rust function that takes an optional integer and prints its value if it exists.
18. Write a Rust function that returns a success value for positive numbers and an error value for negative numbers.
19. Write a Rust program that handles the result of a division operation, using pattern matching to distinguish between success and failure.
Rust Code Editor:
More to Come !
Do not submit any solution of the above exercises at here, if you want to contribute go to the appropriate exercise page.
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics