From the course: Rust Essential Training

Unlock the full course today

Join today to access over 22,400 courses taught by industry experts or purchase this course individually.

Derive traits

Derive traits - Rust Tutorial

From the course: Rust Essential Training

Start my 1-month free trial

Derive traits

- [Instructor] When you define a new custom data type with a struct, by default, the new struck does not implement any traits. It's your job as the programmer to explicitly give it the traits it needs. Consider the example shown here which instantiates two satellites to represent the Hubble space telescope and a GPS satellite and then compares them on line 15 to determine if Hubble and GPS are equal. If I try to run this program, we get a compiler error. It says that the equality operator cannot be applied to the satellite type. And it has a note suggesting that an implementation of the partial equality trait might be missing. We've encountered similar errors earlier in this course. Our custom satellite struct doesn't automatically implement the partial equality trait which provides the ability to compare satellites to each other to see if they're equal. We could implement this trait for our satellite struct. Like we…

Contents