I'm playing with the code on this page.
https://doc.rust-lang.org/rust-by-example/generics/where.html
use std::fmt::Debug;
trait PrintInOption {
fn print_in_option(self);
}
impl<T> PrintInOption for T where
Option<T>: Debug
{
fn print_in_option(self)
{
println!("{:?}", Some(self));
}
}
I wanted a version which wouldn't move the object calling print_in_option
, so I changed self
to &self
:
use std::fmt::Debug;
trait PrintInOption {
fn print_in_option(&self);
}
impl<T> PrintInOption for T where
Option<T>: Debug
{
fn print_in_option(&self)
{
println!("{:?}", Some(self));
}
}
However, this second version won't compile without an additional trait bound. It needs T: Debug
too.
error[E0277]: `T` doesn't implement `Debug`
--> r-14.rs:57:26
|
57 | println!("{:?}", Some(self));
| ^^^^^^^^^^ `T` cannot be formatted using `{:?}` because it doesn't implement `Debug`
I am curious why this extra bound is required based on the change to &self
.