Practice 50 Structs and Enums Coding Questions, TechnoVlogs

Practice 50 Structs and Enums Coding Questions


Q1. Write a Rust program to define a struct Person with fields name and age and create an instance of it.  
Input:  
Name: Alice, Age: 25  
Expected Output:  
Name: Alice, Age: 25

Q2. Write a Rust program to demonstrate how to update fields of a struct.  
Input:  
Original Age: 30, Updated Age: 35  
Expected Output:  
Updated Person: Age: 35

Q3. Write a Rust program to define a tuple struct Color and create an instance.  
Input:  
RGB: (255, 0, 0)  
Expected Output:  
Color: (255, 0, 0)

Q4. Write a Rust program to define an enum Direction with variants Up, Down, Left, and Right. Print a message for each direction.  
Input:  
Direction: Left  
Expected Output:  
Moving Left

Q5. Write a Rust program to implement a method for a struct Rectangle to calculate its area.  
Input:  
Width: 5, Height: 10  
Expected Output:  
Area: 50

Q6. Write a Rust program to define a struct with a tuple field and access its elements.  
Input:  
Point: (2, 3)  
Expected Output:  
x: 2, y: 3

Q7. Write a Rust program to use an enum to store different types of shapes and calculate their area.  
Input:  
Shape: Circle, Radius: 7  
Expected Output:  
Area of Circle: 153.94

Q8. Write a Rust program to define an enum with associated data for a status code.  
Input:  
Status: Success, Code: 200  
Expected Output:  
Status: Success (Code 200)

Q9. Write a Rust program to define a struct with optional fields using Option<T>.  
Input:  
Name: Bob, Age: None  
Expected Output:  
Name: Bob, Age: Not specified

Q10. Write a Rust program to use a struct inside another struct.  
Input:  
Employee: { Name: Alice, Department: HR }  
Expected Output:  
Employee Alice works in HR

Q11. Write a Rust program to implement an enum with a method that matches on its variants.  
Input:  
Shape: Rectangle, Width: 5, Height: 10  
Expected Output:  
Area of Rectangle: 50

Q12. Write a Rust program to destructure a struct and access its fields.  
Input:  
Name: Charlie, Age: 28  
Expected Output:  
Name: Charlie, Age: 28

Q13. Write a Rust program to use impl to add methods to a struct.  
Input:  
Rectangle: Width 4, Height 6  
Expected Output:  
Area: 24

Q14. Write a Rust program to demonstrate how enums with Option<T> are used for null safety.  
Input:  
Value: None  
Expected Output:  
No value provided

Q15. Write a Rust program to implement a method for a struct Point that calculates the distance between two points.  
Input:  
Point1: (0, 0), Point2: (3, 4)  
Expected Output:  
Distance: 5

Q16. Write a Rust program to create a constant using a struct.  
Input:  
None  
Expected Output:  
Constant Point: (0, 0)

Q17. Write a Rust program to define an enum with nested enums for a complex structure.  
Input:  
Variant: Error, Subcode: Timeout  
Expected Output:  
Error: Timeout

Q18. Write a Rust program to use pattern matching to destructure an enum.  
Input:  
Enum: Some(42)  
Expected Output:  
Value: 42

Q19. Write a Rust program to derive Debug for a struct and print its instance.  
Input:  
Person: { Name: John, Age: 30 }  
Expected Output:  
Person { name: "John", age: 30 }

Q20. Write a Rust program to define a struct with private fields and provide getter methods.  
Input:  
Employee: { ID: 123, Salary: 50000 }  
Expected Output:  
ID: 123, Salary: 50000

Q21. Write a Rust program to define a struct Car with fields make, model, and year. Create an instance and print its details.  
Input:  
Make: Toyota, Model: Corolla, Year: 2020  
Expected Output:  
Car: Toyota Corolla, Year: 2020

Q22. Write a Rust program to demonstrate creating an enum Color with variants Red, Green, and Blue, and match on the enum to print a message.  
Input:  
Color: Green  
Expected Output:  
The color is Green

Q23. Write a Rust program to create a struct Rectangle with methods to calculate area and perimeter.  
Input:  
Width: 5, Height: 10  
Expected Output:  
Area: 50, Perimeter: 30

Q24. Write a Rust program to implement an enum Payment with variants Cash and CreditCard, and match on it to process a payment.  
Input:  
Payment: Cash  
Expected Output:  
Processing cash payment

Q25. Write a Rust program to define a struct Person with name, age, and email fields. Use a method to return the email.  
Input:  
Name: Alice, Age: 30, Email: "alice@example.com"  
Expected Output:  
Email: alice@example.com

Q26. Write a Rust program to implement an enum Season with variants Winter, Spring, Summer, and Fall, and match on it to print the season's message.  
Input:  
Season: Summer  
Expected Output:  
It's Summer! Time to relax.

Q27. Write a Rust program to define a struct Book with fields title and author. Implement a method to display the details of the book.  
Input:  
Title: "The Rust Programming Book", Author: "Steve"  
Expected Output:  
Book Title: "The Rust Programming Book", Author: Steve

Q28. Write a Rust program to define an enum Weekday and implement a function to check if a given day is a weekend.  
Input:  
Day: Monday  
Expected Output:  
Monday is not a weekend.

Q29. Write a Rust program to create an enum OrderStatus with variants Pending, Shipped, and Delivered. Print a message based on the order's status.  
Input:  
OrderStatus: Shipped  
Expected Output:  
Your order is shipped.

Q30. Write a Rust program to define a struct Circle with a radius field and implement a method to calculate the circumference.  
Input:  
Radius: 10  
Expected Output:  
Circumference: 62.83

Q31. Write a Rust program to create an enum Response with variants Success and Error, and handle both cases using pattern matching.  
Input:  
Response: Success  
Expected Output:  
Operation successful.

Q32. Write a Rust program to define a struct Student with fields name, age, and marks. Create an instance and print the details.  
Input:  
Name: John, Age: 18, Marks: 90  
Expected Output:  
Student Name: John, Age: 18, Marks: 90

Q33. Write a Rust program to define an enum TrafficLight with variants Red, Yellow, and Green, and print a message based on the traffic light.  
Input:  
TrafficLight: Yellow  
Expected Output:  
Slow down, Yellow light.

Q34. Write a Rust program to define a struct Employee with id, name, and position. Use a method to print employee details.  
Input:  
ID: 101, Name: Bob, Position: Manager  
Expected Output:  
Employee ID: 101, Name: Bob, Position: Manager

Q35. Write a Rust program to define an enum PaymentMethod with variants Cash and Card and implement a function to return a message based on the payment method.  
Input:  
PaymentMethod: Card  
Expected Output:  
Payment made via Card.

Q36. Write a Rust program to create a struct Address and embed it in another struct User. Print the address of a user.  
Input:  
User: Name: "Alice", Address: "123 Main St"  
Expected Output:  
User Address: 123 Main St

Q37. Write a Rust program to define a struct Book with a method that accepts a string and sets the title.  
Input:  
Title: "Rust Basics"  
Expected Output:  
Book Title: Rust Basics

Q38. Write a Rust program to define an enum Fruit with variants Apple, Banana, and Orange, and print a message for each fruit.  
Input:  
Fruit: Apple  
Expected Output:  
You selected Apple.

Q39. Write a Rust program to define a struct Person with a method that returns the age.  
Input:  
Name: "Bob", Age: 25  
Expected Output:  
Age: 25

Q40. Write a Rust program to use enums to define different vehicle types and calculate their fuel efficiency.  
Input:  
Vehicle: Car, Efficiency: 25 mpg  
Expected Output:  
Car fuel efficiency: 25 mpg

Q41. Write a Rust program to implement an enum Response with variants Success and Failure. Display a success or failure message based on the response.  
Input:  
Response: Failure  
Expected Output:  
Operation failed.

Q42. Write a Rust program to define a struct Date with fields day, month, and year. Print the formatted date.  
Input:  
Day: 1, Month: 1, Year: 2025  
Expected Output:  
Date: 1/1/2025

Q43. Write a Rust program to create a struct Person with a method to compare two Person structs by age.  
Input:  
Person 1: Alice, Age: 25  
Person 2: Bob, Age: 30  
Expected Output:  
Bob is older than Alice.

Q44. Write a Rust program to define a struct Product with id, name, and price, and implement a method to apply a discount to the price.  
Input:  
Price: 100, Discount: 20%  
Expected Output:  
Discounted Price: 80

Q45. Write a Rust program to implement an enum AccountStatus with variants Active, Inactive, and Suspended, and match on it to display status messages.  
Input:  
AccountStatus: Active  
Expected Output:  
Account is active.

Q46. Write a Rust program to define a struct Circle with a field radius and implement a method to calculate the area.  
Input:  
Radius: 5  
Expected Output:  
Area: 78.54

Q47. Write a Rust program to define a struct Person with a method to update their age.  
Input:  
Name: John, Age: 25  
Expected Output:  
Updated Age: 26

Q48. Write a Rust program to define an enum Status with variants Started, InProgress, and Completed, and print a message based on the status.  
Input:  
Status: InProgress  
Expected Output:  
Task is in progress.

Q49. Write a Rust program to create a struct Shape with fields width and height. Implement a method to check if it's a square.  
Input:  
Width: 5, Height: 5  
Expected Output:  
It is a square.

Q50. Write a Rust program to define a struct Car and implement a method to check if the car is electric.  
Input:  
Car: { Make: Tesla, Electric: true }  
Expected Output:  
Car is electric.

Share on Social Media