Go On...

We'll cover the following...

Let’s look at another example. I want to loop over a bunch of people. I want to say hi to all of the doctors, but not the scientists. Sounds easy enough:

Press + to interact
enum Job {
Doctor,
Scientist,
}
struct Person {
name: String,
job: Job,
}
fn main() {
let people = [
Person { name: "Alice".to_owned(), job: Job::Doctor },
Person { name: "Bob".to_owned(), job: Job::Scientist },
Person { name: "Charlie".to_owned(), job: Job::Doctor },
];
for person in &people {
match person.job {
Job::Doctor => {
println!("Hello, {}", person.name);
}
Job::Scientist => {
// don't do anything
}
}
}
}

But now, let’s say that I want to add a new job type, Teacher. And I want to greet ...

Access this course and 1400+ top-rated courses and projects.