Syntax Doubts: Trait Bound, Implementation Declaration

โš“ Rust    ๐Ÿ“… 2025-09-30    ๐Ÿ‘ค surdeus    ๐Ÿ‘๏ธ 13      

surdeus

Warning

This post was published 112 days ago. The information described in this article may have changed.

Questions 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 (){ } 
}
  1. Why isn't T:Trait inferred, so that we only need impl<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

Read full topic

๐Ÿท๏ธ Rust_feed