w3resource

Rust Variables and Data Types Exercises with Solutions and Explanations

Rust Variables and Data Types Exercises [10 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 declares a variable birth_year and calculates the age based on the current year and the user's birth year.

Click me to see the solution

2. Write a Rust function that calculates the circumference of a circle using the constant PI and a given diameter.

Click me to see the solution

3. Write a Rust function that accepts a mutable reference to a counter variable and increments it by a specified amount.

Click me to see the solution

4. Write a Rust program that declares a boolean variable is_sunny and sets it to true if it's sunny and false otherwise.

Click me to see the solution

5. Write a Rust program that creates an integer variable temp and assign it a temperature value in Celsius. Convert it to Fahrenheit and print the result.

Click me to see the solution

6. Write a Rust program that defines a string variable name and assigns it your name. Print a greeting message using the name variable.

Click me to see the solution

7. Write a Rust program that defines a tuple of coordinates representing latitude and longitude. Print each tuple element separately.

Click me to see the solution

8. Write a Rust program that creates an array of numbers containing 5 integers. Iterate over the array and print each element.

Click me to see the solution

9. Write a Rust program that defines a character variable and assigns it the first letter of your first name.

Click me to see the solution

10. Write a Rust program that creates a vector of items containing different types of items. Print each item in the vector.

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.



Follow us on Facebook and Twitter for latest update.