The reason the existing bound doesn't work is that Option<&T> is a different type from Option<T>. The reason it asks for T: Debug rather than Option<&T>: Debug is that Option<&T> is Debug if and only if T is Debug, and T: Debug is simpler.
I see. Is a version with Option<&T>: Debug salvageable? When I try, I am getting:
error[E0637]: `&` without an explicit lifetime name cannot be used here
--> r-14.rs:53:12
|
53 | Option<&T>: std::fmt::Debug,
| ^ explicit lifetime name needed here
impl<T> PrintInOption for T
where
for<'a> Option<&'a T>: Debug,
{
fn print_in_option(&self) {
println!("{:?}", Some(self));
}
}
This is necessary because Option<&'a T> and Option<&'b T> are different types. The for<'a> syntax means "for all lifetimes", and is equivalent to this:
where the list is infinitely long, going through all possible lifetimes.
(strictly speaking you only need Option<&'that_lifetime_inside_print_in_option T>: Debug, but it is not possible to talk about that lifetime, so we have to instead say it applies to all lifetimes)