Start learning free

LESSON 101 OF 293 · CONDITIONAL STATEMENTS - CHALLENGES

Employee Of The Year V2 in Rust: Two Employees and if/else

Practice Read the full explanation

Two employees are up for the Employee of the Year award. Pick the more productive one and compute 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;

Build:

Mary Jane
Bonus: $50250

Rules:

  • The winner is whichever employee has the higher productivity.
  • If both have the same productivity, employee 1 (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 if/else to assign them, so both are still in scope for the prints. A tie goes to employee 1, so compare with >=.

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 winner_name;
    let winner_salary;
    if productivity1 >= productivity2 {
        winner_name = name1;
        winner_salary = salary1;
    } else {
        winner_name = name2;
        winner_salary = salary2;
    }
    let bonus = winner_salary * 15 / 100;
    println!("{}", winner_name);
    println!("Bonus: ${}", bonus);

STEP 1 OF 10
    Ctrl + Enter to check

    RUST · CONDITIONAL STATEMENTS - CHALLENGES

    Employee Of The Year V2 in Rust, explained

    Problem overview

    Version 2 of the project has two employees, each with a name, a productivity score and a salary. The more productive one wins the award, and the program prints the winner's name and their 15% bonus, rounded down. If the two scores are equal, the employee listed first wins.

    In the example, John Doe has productivity 83 and Mary Jane has 133. Mary wins, and her bonus is 335000 * 15 / 100 = 50250:

    Mary Jane
    Bonus: $50250
    

    Version 1 computed the bonus for a single fixed employee. The new part is choosing which employee's data to use.

    Approach: let the branches fill in the winner

    The bonus formula is the same for both employees, so the program only has to decide whose name and salary go into it. An if/else compares the productivities. In each branch the program stores the winner's name and salary in two shared variables. After the branches, one bonus calculation and two prints work for either winner.

    Rust allows a variable to be declared without a value, as long as every path through the code gives it one before it is read. That fits this problem well: winner_name and winner_salary are declared first and assigned inside the if and the else. The compiler checks that no path leaves them empty.

    The tie rule decides the comparison. Employee 1 wins on equal scores, so the if asks whether productivity1 >= productivity2. Everything else goes to employee 2.

    It is tempting to compute and print the bonus inside each branch. That works, but the formula and the two println! calls would then appear twice, and a later change to the bonus rate would have to be made in both places. Computing the bonus once, after the branches, keeps a single copy of that logic, and the branches only decide whose data flows into it.

    Step by step

    1. Declare the winner's variables. Write let winner_name; and let winner_salary; with no value. They have to be declared before the if, because variables created inside a branch disappear at its closing brace.
    2. Write the condition. Open an if that tests productivity1 >= productivity2. The tie rule is the reason for >= instead of >.
    3. Fill in employee 1. Inside the if, assign name1 to winner_name and salary1 to winner_salary. Keeping the pair together means the bonus always comes from the same person as the name.
    4. Add the else. It runs when employee 2 is strictly more productive.
    5. Fill in employee 2. Inside the else, assign name2 and salary2 the same way.
    6. Compute the bonus. After the if/else, set bonus to winner_salary * 15 / 100. Multiplying first and dividing second keeps the whole-number result rounded down.
    7. Print. Print winner_name on its own line, then Bonus: $ followed by bonus.

    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 winner_name;
        let winner_salary;
        if productivity1 >= productivity2 {
            winner_name = name1;
            winner_salary = salary1;
        } else {
            winner_name = name2;
            winner_salary = salary2;
        }
        let bonus = winner_salary * 15 / 100;
        println!("{}", winner_name);
        println!("Bonus: ${}", bonus);
    

    Rust details worth a closer look:

    • Deferred initialization. let winner_name; has no type written, and Rust works it out from the later assignment. The variable doesn't need mut, because each branch assigns it exactly once.
    • Both branches must assign. If the else were missing and the code read winner_name afterwards, the compiler would refuse: used binding `winner_name` is possibly-uninitialized. The error even points out that an else arm might be missing.
    • The condition has no parentheses. Rust doesn't require them around an if condition, and the condition must be a bool. A number is not accepted as true or false.
    • Conditions in Rust are expressions. You could write let winner_name = if ... { name1 } else { name2 };, but with two values to set, two assignments in each branch read more clearly.

    Edge cases and common mistakes

    • Ties. With equal productivity, employee 1 must win. With >=, scores of 100 and 100 give John Doe a bonus of 20250. With > the same input gives Mary Jane and 50250.
    • Setting only one of the two variables. If a branch assigns the name but not the salary, the bonus calculation is rejected by the compiler. That check catches mismatched winners at compile time.
    • Using the loser's salary. The name and salary must come from the same branch, otherwise the printed name and bonus belong to different people.
    • Assigning twice. Without mut, a variable can be given a value only once on any path. Assigning winner_name before the if and again inside it fails with cannot assign twice to immutable variable.

    Testing

    • Productivities 83 and 133 give Mary Jane and Bonus: $50250.
    • Productivities 150 and 100 with the same salaries give employee 1, with a bonus of 20250.
    • Productivities 100 and 100 give employee 1, with a bonus of 20250.
    Keep going with Rust1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free