w3resource

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.

Click me to see the solution

2. Write a Rust program that accepts two numbers from the user, adds them together, and displays the result.

Click me to see the solution

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.

Click me to see the solution

4. Write a Rust program that converts an integer to a string and vice versa and prints the result.

Click me to see the solution

5. Write a Rust program that performs basic Math operations - addition, subtraction, multiplication, and division operations on two integers.

Click me to see the solution

6. Write a Rust program that checks if a number is even or odd and prints the result.

Click me to see the solution

7. Write a Rust program that counts from 1 to 10 using a loop and prints each number.

Click me to see the solution

8. Write a Rust program that prints all even numbers from 1 to 20 using a while loop.

Click me to see the solution

9. Write a Rust program that prints all elements of an array using a for loop.

Click me to see the solution

10. Write a Rust program that defines a function that calculates the factorial of a given number and returns the result.

Click me to see the solution

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.

Click me to see the solution

12. Write a Rust function that takes a reference to a variable as a parameter and modifies its value.

Click me to see the solution

13. Write a Rust function that takes two string references and returns the smallest one.

Click me to see the solution

14. Write a Rust program that defines a struct Person with fields like name and age.

Click me to see the solution

15. Write a Rust program that creates an instance of the Person struct and prints its fields.

Click me to see the solution

16. Write a Rust program that defines an enum Color with variants representing different colors.

Click me to see the solution

17. Write a Rust function that takes an optional integer and prints its value if it exists.

Click me to see the solution

18. Write a Rust function that returns a success value for positive numbers and an error value for negative numbers.

Click me to see the solution

19. Write a Rust program that handles the result of a division operation, using pattern matching to distinguish between success and failure.

Click me to see the solution

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.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/rust/basic/basic-exercises-for-beginners.php