Deriving PartialEq
Enums can be useful to compare different states of a type, for example if status == OrderStatus::Pending. However, you can not directly compare two enums for equality.
For this to work, you need to implement the PartialEq trait for the enum.
This can be done manually, but Rust provides a convenient way to implement the PartialEq trait for types using the derive attribute. This allows you to compare instances of a type for equality without manually implementing the trait.
In this challenge, you'll define a simple enum and use #[derive(PartialEq)] to automatically implement equality checks.
Your Task
-
Define an enum
OrderStatuswith the following variants:Pending— a unit variant representing an order that is not yet processed.Shipped— a unit variant representing an order that has been shipped.Cancelled(String)— a tuple variant with a reason for cancellation.
-
Use the
#[derive(PartialEq)]attribute to derive thePartialEqtrait for the enum.
Requirements
- Use the
#[derive(PartialEq)]macro for theOrderStatusenum.
Hints
Click here to reveal hints
- Use the
derivemacro on the enum to automatically implementPartialEq. - String types in Rust already implement
PartialEq, soCancelled(String)can be compared automatically.
No run yet
Press
Run to execute your code and see test results here.