Structs: A Generic Set


The following genericset program rewrites the intset example using generics. Instead of a set of i32 values, it defines GenericSet<T>, a set whose values have some type T chosen by the code that uses it. The program is very close to line-for-line the same as intset; compare the two to see what changed:

One line needs a preview of a later topic. The functions compare values with == and duplicate them with .clone(), which not every type allows, so the impl block is written impl<T: PartialEq + Clone>. PartialEq and Clone are traits, which we will cover later. For now, read this as “any type T that can be compared and cloned.”

This zip file contains the complete crate.

src/main.rs