Rust Structs and Enums Exercises with Solutions and Explanations
Rust Structs and Enums 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 defines a struct Rectangle with fields width and height. Write a function to calculate the area of a rectangle instance.
2. Write a Rust program that creates an enum Direction with variants representing different directions (e.g., North, South, East, West).
3. Write a Rust program that defines a struct Point with fields x and y. Now write a function to calculate the distance between two points.
4. Write a Rust program that implements an enum Shape with variants representing different geometric shapes (e.g., Circle, Triangle, Square).
5. Write a Rust program that defines a struct Student with fields like name, age, and email. The next step is to write a function that prints out the student's details.
6. Write a Rust program that creates an enum Result with variants representing success and failure states.
7. Write a Rust program that defines a struct Employee with fields id, name, and salary. Now write a function to calculate an employee's annual salary.
8. Write a Rust program that implements an enum Color with variants representing different colors (e.g., Red, Green, Blue).
9. Write a Rust program that defines a struct Car with fields make, model, and year. Now write a function to print the details of a car.
10. Write a Rust program that creates an enum Option with variants representing some and none states.
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.
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://w3resource.com/rust/basic/structs-and-enums.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics