Start learning free

LESSON 102 OF 293 · CONDITIONAL STATEMENTS - CHALLENGES

Employee Of The Year V3 in Rust: Three Employees with else if

Practice Read the full explanation

Three employees compete for the award. Use a chained if / else if / else to pick the most productive one and print their 15% bonus.

You start with:

    let name1 = "John Doe";
    let productivity1 = 83;
    let salary1 = 135000;
    let name2 = "Mary Jane";
    let productivity2 = 133;
    let salary2 = 335000;
    let name3 = "Bob Smith";
    let productivity3 = 97;
    let salary3 = 210000;

Build:

Mary Jane
Bonus: $50250

Rules:

  • The winner is whichever of the three employees has the highest productivity.
  • If two or more employees share the highest productivity, the one listed first wins.
  • The winner's bonus is 15% of their salary, rounded down to a whole number.
  • Print the winner's name on the first line, then "Bonus: $<amount>" on the second.

Declare let winner_name; let winner_salary; before the if, then use an if / else if / else chain to assign them, so both are still in scope for the prints. Employee 1 wins when its productivity is at least that of both others; if not, employee 2 wins when it is at least employee 3's; otherwise employee 3 wins.

Dividing two whole numbers (i32) drops the fraction, so winner_salary * 15 / 100 is already rounded down: let bonus = winner_salary * 15 / 100;

{} in the format string is replaced by the value: println!("Bonus: ${}", bonus);

    let name1 = "John Doe";
    let productivity1 = 83;
    let salary1 = 135000;
    let name2 = "Mary Jane";
    let productivity2 = 133;
    let salary2 = 335000;
    let name3 = "Bob Smith";
    let productivity3 = 97;
    let salary3 = 210000;

    let winner_name;
    let winner_salary;
    if productivity1 >= productivity2 && productivity1 >= productivity3 {
        winner_name = name1;
        winner_salary = salary1;
    } else if productivity2 >= productivity3 {
        winner_name = name2;
        winner_salary = salary2;
    } else {
        winner_name = name3;
        winner_salary = salary3;
    }
    let bonus = winner_salary * 15 / 100;
    println!("{}", winner_name);
    println!("Bonus: ${}", bonus);

STEP 1 OF 13
    Ctrl + Enter to check

    RUST · CONDITIONAL STATEMENTS - CHALLENGES

    Employee Of The Year V3 in Rust, explained

    Problem overview

    Three employees compete for the award. Each has a name, a productivity score and a salary. The program finds the one with the highest productivity and prints their name and their bonus, which is 15% of the salary rounded down. If two or more employees share the top score, the one listed first wins.

    With John Doe at 83, Mary Jane at 133 and Bob Smith at 97, Mary has the highest score. Her bonus is 335000 * 15 / 100 = 50250:

    Mary Jane
    Bonus: $50250
    

    The previous version chose between two employees with a single if/else. With three, one comparison isn't enough, so the lesson uses a chain of if, else if and else.

    Approach: eliminate one candidate at a time

    Think of the chain as a sequence of questions that each remove someone from the race:

    1. Is employee 1 at least as productive as both of the others? If so, employee 1 wins.
    2. If not, employee 1 has lost to somebody, so only employees 2 and 3 remain. Is employee 2 at least as productive as employee 3? If so, employee 2 wins.
    3. Otherwise employee 3 wins, with no further test needed.

    The chain stops at the first branch whose condition is true, so the second question is asked only when the first answer was no. That is why the second condition can be as short as productivity2 >= productivity3: employee 1 has already been ruled out.

    Every comparison uses >= rather than >. On a tie, the earlier employee must win, and >= lets the earlier employee in the earlier branch claim the tie.

    Step by step

    1. Declare the winner's variables without values: winner_name and winner_salary. They must exist before the chain so the code after it can read them.
    2. Test employee 1. The condition has two comparisons joined by &&: productivity1 must be at least productivity2 and at least productivity3.
    3. Fill in employee 1's name and salary inside that branch. Keep them together so the bonus matches the name.
    4. Add an else if for employee 2 that tests productivity2 >= productivity3.
    5. Fill in employee 2's name and salary inside it.
    6. Add the final else. Reaching it means employees 1 and 2 both lost, so employee 3 is the winner.
    7. Fill in employee 3's name and salary.
    8. Compute the bonus once, after the chain, as winner_salary * 15 / 100.
    9. Print the name on one line and Bonus: $ with the amount on the next.

    The solution in Rust

        let name1 = "John Doe";
        let productivity1 = 83;
        let salary1 = 135000;
        let name2 = "Mary Jane";
        let productivity2 = 133;
        let salary2 = 335000;
        let name3 = "Bob Smith";
        let productivity3 = 97;
        let salary3 = 210000;
    
        let winner_name;
        let winner_salary;
        if productivity1 >= productivity2 && productivity1 >= productivity3 {
            winner_name = name1;
            winner_salary = salary1;
        } else if productivity2 >= productivity3 {
            winner_name = name2;
            winner_salary = salary2;
        } else {
            winner_name = name3;
            winner_salary = salary3;
        }
        let bonus = winner_salary * 15 / 100;
        println!("{}", winner_name);
        println!("Bonus: ${}", bonus);
    

    Rust details to know:

    • Every path assigns both variables. The let winner_name; form with no value is accepted only because the if, the else if and the else each assign it. If you left out the final else, the compiler would reject the later use as possibly uninitialized, because the chain could end without any branch running.
    • && short-circuits. If productivity1 >= productivity2 is false, the second comparison is never evaluated. Both operands must be bool values, and comparing two numbers produces one.
    • else if is just else followed by another if. There is no separate keyword like elif.
    • Operands are the same type. All three productivities are i32 through inference, so they compare directly. Rust doesn't compare an integer with a different integer type without a cast.

    Edge cases and common mistakes

    • Dropping the && part. A chain that starts with only productivity1 >= productivity2 picks employee 1 whenever they beat employee 2, even if employee 3 is better. With productivities 100, 50 and 120 that chain answers employee 1, while the correct winner is employee 3.
    • Ties. For 100, 100 and 100 the winner is employee 1. For 50, 90 and 90 it is employee 2, because the first if fails and employee 2 meets >= against employee 3.
    • Using > in any comparison. That hands the tie to a later employee and breaks the rule that the first listed wins.
    • Repeating the bonus code in each branch. It works, but a single calculation after the chain is shorter and can't drift out of sync between branches.
    • Forgetting one of the two assignments in a branch, so the name and salary come from different employees or the compiler complains about an uninitialized variable.

    Testing

    • Productivities 83, 133, 97 give employee 2 and Bonus: $50250.
    • Productivities 100, 100, 100 give employee 1.
    • Productivities 50, 90, 90 give employee 2.
    • Productivities 50, 60, 90 give employee 3.
    • Productivities 120, 90, 100 give employee 1.
    Keep going with Rust1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free