From the course: Rust Essential Training
Unlock this course with a free trial
Join today to access over 25,600 courses taught by industry experts.
Derive traits - Rust Tutorial
From the course: Rust Essential Training
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…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.
Contents
-
-
-
-
-
-
-
-
-
-
-
-
-
-
(Locked)
Implement traits4m 33s
-
(Locked)
Default trait implementation1m 33s
-
(Locked)
Derive traits3m 58s
-
(Locked)
Trait bounds3m 8s
-
(Locked)
Multiple trait bounds5m 16s
-
(Locked)
Return types with implemented traits3m 40s
-
(Locked)
Challenge: Implement the display trait1m 16s
-
(Locked)
Solution: Implement the display trait1m 53s
-
(Locked)
-
-
-
-
-