Consider the following code:
pub trait Trait { type Type; const CONST: Self::Type; } impl<T> Trait for T { type Type = u8; const CONST: u8 = 42; } My (incorrect?) understanding of Rust is that this code should work and that all Sized types should now implement Trait and have an associated type (Type = u8) and const (CONST = 42). Unsized types shouldn't implement this trait since impl<T> implicitly assumes T to be Sized.
However, when I try to compile the code I get the error message:
error[E0277]: the size for value values of type `T` cannot be known at compilation time --> src/main.rs:8:3 | 8 | const CONST: u8 = 42; | ^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time | = help: the trait `std::marker::Sized` is not implemented for `T` = note: to learn more, visit <https://doc.rust-lang.org/book/second-edition/ch19-04-advanced-types.html#dynamically-sized-types--sized> = help: consider adding a `where T: std::marker::Sized` bound = note: required because of the requirements on the impl of `Trait` for `T` My questions:
- Why does Rust think that
Tisn'tSizedhere? Explicitly statingT: Sizeddoesn't help. - Let's pretend
Tisn'tSized. Why does Rust care whetherTis sized or not here? Nothing depends on it, as far as I can tell (the associated type and const aren't related toT). Changing the code toT: ?Sizedworks, so clearlyTbeing unsized isn't actually problematic.
impl<T: Sized> Trait for T? (This doesn't solve the problem but matches your problem description more)T: Sizedis redundant: All type parameters have an implicit bound ofSized..