A derive is a Rust compiler directive that generates an implementation of a selected trait for a type. Deriving Debug lets Rust print an Error value in a developer-useful representation. section 2.5.6
The FizzBuzz error path shows the practical progression:
- Calling unwrap on Result requires the error type to implement Debug; without it, compilation fails. section 2.5.6
- Adding #[derive(Debug)] to Error satisfies that requirement and makes the panic include GotNegative. section 2.5.6
- Derive simplifies this common implementation, while larger programs still need proper error handling instead of routine panics. section 2.5.6
Deriving Debug for an error enum
#[derive(Debug)] enum Error { GotNegative, } fn print_fizzbuzz(x: i32) { match fizzbuzz(x).unwrap() { // handle the successful FizzBuzz result } } With Debug derived, a negative input panics with the error value: GotNegative. section 2.5.6
Listing 2.37 Panicking when print_fizzbuzz sees an error
enum FizzBuzzValue {
Fizz,
Buzz,
FizzBuzz,
NotDivisible(i32),
}
enum Error {
GotNegative,
}
fn main() {
print_fizzbuzz(-1); #1
}
fn print_fizzbuzz(x: i32) { #2
match fizzbuzz(x) {
Ok(result) => match result {
FizzBuzzValue::FizzBuzz => {
println!("FizzBuzz");
}
FizzBuzzValue::Fizz => {
println!("Fizz");
}
FizzBuzzValue::Buzz => {
println!("Buzz");
}
FizzBuzzValue::NotDivisible(num) => {
println!("{}", num);
}
#3
},
Err(Error::GotNegative) => {
panic!("Got a negative number for fizzbuzz: {}", x);
}
}
}
fn fizzbuzz(x: i32) -> Result<FizzBuzzValue, Error> {
if x < 0 {
Err(Error::GotNegative)
} else if x % 3 == 0 && x % 5 == 0 {
Ok(FizzBuzzValue::FizzBuzz)
} else if x % 3 == 0 {
Ok(FizzBuzzValue::Fizz)
} else if x % 5 == 0 {
Ok(FizzBuzzValue::Buzz)
} else {
Ok(FizzBuzzValue::NotDivisible(x))
}
}
Compare the original FizzBuzz error-handling code with the shorter unwrap-based form that uses a derived Debug implementation. section 2.5.6




