Why are unsized types allowed in trait method declarations? For example, this code compiles:
trait Blah {
fn blah(&self, input: [u8]) -> dyn Display;
}
But implementing Blah is impossible:
impl Blah for Foo {
fn blah(&self, input: [u8]) -> dyn Display {
"".to_string()
}
}
// error[E0277]: the size for values of type `(dyn std::fmt::Display + 'static)` cannot be known at compilation time
// error[E0277]: the size for values of type `[u8]` cannot be known at compilation time
Giving blah a default implementation is also impossible:
trait Blah {
fn blah(&self, input: dyn Display) -> dyn Display { "".to_string() }
}
// error[E0277]: the size for values of type `(dyn std::fmt::Display + 'static)` cannot be known at compilation time
Nested unsized types are also not allowed. This inconsistency makes me think this is a compiler bug:
trait Blah {
fn blah(&self, input: [str]) -> dyn Display;
}
// error[E0277]: the size for values of type `str` cannot be known at compilation time
I found a couple old GitHub issues that claim this behavior is intentional, but I could not find a reason for it. Why is this intentional behavior? If implementing a trait of this nature is impossible, why doesn't the compiler catch it in the trait declaration?