Question about Trait Bounds

pub trait TestTrait {
    fn test() {}
}

#[derive(Default)]
pub struct OtherStruct;

impl TestTrait for OtherStruct {}

#[derive(Default, Debug)]
pub struct StructTest {
    a: u32,
    b: u32,
    c: Vec<u32>,
}

impl TestTrait for StructTest {}

#[derive(Default, Debug)]
pub struct ZZ<T: TestTrait = StructTest> {
    e: T,
}

comfuse about "T: TestTrait = StructTest",why not just "T:impl TestTrait"?
i can put any type that impl TestTrait into T,there is no error with compile.
but when T is StructTest,the type is ZZ,if not,wiil be like ZZ< Type >.
like this:

pub fn main{
     let  z:ZZ = ZZ {
        e: StructTest::default(),
    };
    let z:ZZ<OtherStruct> = ZZ {
        e: StructTest::default(),
    };
}

the different is:ZZ vs ZZ< OtherStruct >.
i do not understand why "T: TestTrait = StructTest"?
thanks for help!

It's just a default type parameter that gets used only if you don't specify an explicit type parameter.

2 Likes

thanks :slight_smile:

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.