Search⌘ K

Salaries

Explore how to apply early exit strategies in Rust programming to make code clearer and easier to follow. Learn to use return statements and other control flow tools to avoid complex nested matching, improving code readability and maintainability.

We'll cover the following...

Let’s say that teachers make $50, scientists make $70, and students don’t make any money. We can write a method to help with that:

Rust 1.40.0
enum Job {
Teacher,
Scientist,
Student,
}
impl Job {
fn salary(&self) -> Option<u32> {
match self {
Job::Teacher => Some(50),
Job::Scientist => Some(70),
Job::Student => None,
}
}
}

We want to find out how much money two people will make together. But if either of ...