More Teachers
Explore how to define powerful enums in Rust by adding fields to variants. Understand improving type safety and code maintainability when representing complex roles like teachers and scientists. Learn to create enums with multiple fields, write matching methods, and simplify handling different types using pattern matching techniques.
We'll cover the following...
I’m not happy with just talking about teachers. I want to know if the teacher is teaching math or art. Let’s rewrite our Job type as:
This works, but I don’t like it that much. I’ll give you two reasons why; first, let’s say that I want to write a method is_teacher. With the enum above, I can write something like:
As I started adding physics, economics, and other teachers, this is just going to keep growing. I don’t like that. There’s nothing in the Job enum telling me that there’s some ...