I want to call a non-generic private associated function from a non-generic method of a generic struct. I can't do that because Rust cannot infer the type parameter:
use std::fmt::Display;
struct MyStruct<T: Display> {
val: T,
}
impl<T: Display> MyStruct<T> {
pub fn show(&self) {
MyStruct::label();
println!("{}", self.val);
}
fn label() {
print!("val: ");
}
}
fn main() {
let x = 42;
let ms = MyStruct { val: x };
ms.show();
}
Compiling this code gives an error:
error[E0282]: type annotations needed
--> src/main.rs:9:9
|
9 | MyStruct::label();
| ^^^^^^^^^^^^^^^ cannot infer type for type parameter `T`