Using Self as a generic type parameter in a trait
⚓ Rust 📅 2025-10-03 👤 surdeus 👁️ 6Hi,
I have a struct which takes a generic type parameter and I want to create a trait which provides a function which takes self and puts this self into the generic type parameter field of my struct.
The goal is to implement the trait i.e. for String.
But when I compile I get the error about the incorrect type parameter. See below.
struct MyStruct<T> {
text: T,
id: usize
}
impl<T> MyStruct<T> {
fn new(text: T, id: usize) -> Self {
Self { text, id }
}
}
trait MyTrait {
fn foo<T>(self, id: usize) -> MyStruct<T>
where Self: Sized
{
MyStruct::new(self, id)
}
}
fn main() {
let _ = MyStruct::new("abc", 1);
}
Errors:
Compiling playground v0.0.1 (/playground)
error[E0308]: mismatched types
--> src/main.rs:16:23
|
12 | trait MyTrait {
| ------------- found type parameter
13 | fn foo<T>(self, id: usize) -> MyStruct<T>
| - expected type parameter
...
16 | MyStruct::new(self, id)
| ------------- ^^^^ expected type parameter `T`, found type parameter `Self`
| |
| arguments to this function are incorrect
|
= note: expected type parameter `T`
found type parameter `Self`
= note: a type parameter was expected, but a different one was found; you might be missing a type parameter or trait bound
= note: for more information, visit https://doc.rust-lang.org/book/ch10-02-traits.html#traits-as-parameters
note: associated function defined here
--> src/main.rs:7:8
|
7 | fn new(text: T, id: usize) -> Self {
| ^^^ -------
For more information about this error, try `rustc --explain E0308`.
error: could not compile `playground` (bin "playground") due to 1 previous error
3 posts - 2 participants
🏷️ Rust_feed