Good Code
The good version models status as an enum and matches every variant. Adding a new status creates a compiler-guided review point.
Lesson 05
Match enum states directly so new variants force a review of every state-dependent branch.
pub enum ReviewStatus {
Draft,
Approved,
Rejected { reason: String },
}
pub fn status_label(status: &ReviewStatus) -> &'static str {
// Exhaustive match makes every status visible during review.
match status {
ReviewStatus::Draft => "draft",
ReviewStatus::Approved => "approved",
ReviewStatus::Rejected { .. } => "rejected",
}
}pub fn status_label(status: &str) -> &'static str {
// String states let unknown values slip into the default branch.
match status {
"draft" => "draft",
"approved" => "approved",
_ => "unknown",
}
}The good version models status as an enum and matches every variant. Adding a new status creates a compiler-guided review point.
The bad version uses string states and a broad default branch. A misspelled or newly added status becomes "unknown" without forcing a design decision.