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
- Declare the winner's variables. Write
let winner_name;andlet winner_salary;with no value. They have to be declared before theif, because variables created inside a branch disappear at its closing brace. - Write the condition. Open an
ifthat testsproductivity1 >= productivity2. The tie rule is the reason for>=instead of>. - Fill in employee 1. Inside the
if, assignname1towinner_nameandsalary1towinner_salary. Keeping the pair together means the bonus always comes from the same person as the name. - Add the
else. It runs when employee 2 is strictly more productive. - Fill in employee 2. Inside the
else, assignname2andsalary2the same way. - Compute the bonus. After the
if/else, setbonustowinner_salary * 15 / 100. Multiplying first and dividing second keeps the whole-number result rounded down. - Print. Print
winner_nameon its own line, thenBonus: $followed bybonus.
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 needmut, because each branch assigns it exactly once. - Both branches must assign. If the
elsewere missing and the code readwinner_nameafterwards, the compiler would refuse:used binding `winner_name` is possibly-uninitialized. The error even points out that anelsearm might be missing. - The condition has no parentheses. Rust doesn't require them around an
ifcondition, and the condition must be abool. 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. Assigningwinner_namebefore theifand again inside it fails withcannot assign twice to immutable variable.
Testing
- Productivities 83 and 133 give
Mary JaneandBonus: $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.
