Very very strange for me


fn main() {

struct Struct<T> {
    value: T,
trait Trait {}

struct StructB;
impl Trait for StructB {}

struct Wrapper<T: Trait> {
    value: Struct<T>,

impl<T: Trait> Struct<T> {
    fn new(value: T) -> Self {
        Self { value }

    // fn a() {
    //     Self::new(StructB);
    // }

If you uncomments it, this code would lead an error.
Why ? (Please notice main)

Self is the type being implemented, so it's Struct<T>. The method Struct<T>::new() takes a value of type T, which is a generic parameter, so whoever uses the type can choose it. So it doesn't make sense to call it with a value of type StructB – what if someone calls Struct::<NotStructB>::a(), what should happen, then?

Thanks very much!

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.