Syntax Doubts: Trait Bound, Implementation Declaration
โ Rust ๐ 2025-09-30 ๐ค surdeus ๐๏ธ 13Questions are about the syntax of trait bounds. Take the example:
// Valid T must implement the trait Trait
struct Example<T: Trait> {
value: T
}
If we want to implement a method on it:
impl<T:Trait> Example<T> {
fn something (){ }
}
- Why isn't
T:Traitinferred, so that we only needimpl<T> Example <T>? In other words, is this necessary or is it a decision for improving code-readability?
LLM answer: "The compiler does not look into the structโs definition to fetch its bounds. " (which as OP I am aware, but does not answer why.)
Now a closely related example:
impl<T:Display, U:Trait> SomeTrait<T> for Example<U> { }
But this should be allowed:
impl<T, U> SomeTrait<T> for Example<U> { }
Rust could infer: whatever is used on Example<U> is already assumed to have Trait implemented.
Last closely related example. This is forbidden:
impl<T:Trait> A {
fn something(a:T) {}
}
Was confusing as well. But I do get that if T is only used into a function using it in the impl<T:Trait> looks hierarchically / semantically incorrect.
3 posts - 3 participants
๐ท๏ธ Rust_feed