I've the below enum
enum Day {
Monday, Tuesday, Wednesday, Thursday, Friday
}
When I tried doing:
println!("z: {}", Day::Saturday);
I got the below:
error[E0277]: `Day` doesn't implement `std::fmt::Display`
--> bin/src/main.rs:11:31
|
11 | println!("z: {}", Day::Saturday);
| ^^^^^^^^^^^^^ `Day` cannot be formatted with the default formatter
|
= help: the trait `std::fmt::Display` is not implemented for `Day`
= note: in format strings you may be able to use `{:?}` (or {:#?} for pretty-print) instead
= note: required by `std::fmt::Display::fmt`
So, i changed the code to be:
println!("z: {:?}", Day::Saturday);
And getting now the below:
error[E0277]: `Day` doesn't implement `std::fmt::Debug`
--> bin/src/main.rs:11:33
|
11 | println!("z: {:?}", Day::Saturday);
| ^^^^^^^^^^^^^ `Day` cannot be formatted using `{:?}`
|
= help: the trait `std::fmt::Debug` is not implemented for `Day`
= note: add `#[derive(Debug)]` or manually implement `std::fmt::Debug`
= note: required by `std::fmt::Debug::fmt`
This had been resolved by adding #[derive(Debug)]
as:
#[derive(Debug)]
enum Day {
Saturday, Sunday ,Monday, Tuesday, Wednesday, Thursday, Friday
}
I tried to use Debug
and Release
traits implementation instead of using #[derive(Debug)]
, so I wrote the below:
use std::fmt;
impl fmt::Debug for Day {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{:?}", self)
}
}
impl fmt::Display for Day {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{:?}", self)
}
}
And tried it with the below code:
println!("z: {:?}", Day::Saturday);
But got the below error:
thread 'main' has overflowed its stack
fatal runtime error: stack overflow
Abort trap: 6