Trait bounds

A trait can also have a bound placed upon it. In effect, a bound is a rule that the trait has to abide by and is added to the declaring type parameter.

The source for this part is in 09/trait_bound_gen_struct.

In the code example, the impl has a PartialEq bound placed upon the generic type. Our struct contains four parameters within it and so we only want to test for partial equality within that struct. If we didn't have the PartialEq on the declared type parameter, the compilation would fail as we're not testing everything within that struct.

When the code is compiled, we get the following output:

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset