Problem overview
This is the first version of a small project. You have one employee with a name, a productivity score and a salary, and you print the name followed by an end-of-year bonus. The bonus is 15% of the salary, rounded down to a whole number.
For John Doe with a salary of 135000, 15% is 20250, so the program prints two lines: the name, then Bonus: $20250. The productivity score isn't needed yet. Later versions of the project use it to compare employees, so it is already in the data.
Approach: do the percentage with integers
Rust never converts between number types behind your back. The three starting values are whole numbers, so the arithmetic stays in whole numbers. To take 15% of a salary, multiply by 15 first and divide by 100 second: salary * 15 / 100.
Dividing two integers in Rust discards the fraction, so the result is already rounded down. A salary of 135001 gives 20250 as well, because 2025015 divided by 100 is 20250 with the remainder dropped. Nothing else is needed for the rounding rule.
The order of the operations matters. Dividing first, as in salary * (15 / 100), computes 15 / 100 as an integer division, which is 0, so the bonus would be 0 for every salary. Multiply first, so the division works on the large number.
Step by step
- Compute the bonus. Create a variable named
bonusand give itsalary * 15 / 100. Both operators have the same precedence and work left to right, so the multiplication happens before the division. - Print the name. The first output line is the employee's
nameon its own line. - Print the bonus line. The second line is the text
Bonus: $followed by the number. A format string with a{}placeholder puts the value ofbonusafter the dollar sign.
The solution in Rust
let name = "John Doe";
let productivity = 83;
let salary = 135000;
let bonus = salary * 15 / 100;
println!("{}", name);
println!("Bonus: ${}", bonus);
The program prints:
John Doe
Bonus: $20250
A few details are worth knowing:
- Types are inferred. Nothing in the code says
i32, but with no other information Rust gives whole-number literals the typei32.nameis a&str, a borrowed piece of text. {}prints any value that can be displayed. Strings and integers both work, so there is no conversion step like turning the number into text first.$is an ordinary character. In a Rust format string only{and}are special. To print a literal brace you write it twice:{{prints{.- The unused variable warning.
productivityis never read in this version, so the compiler printswarning: unused variable: `productivity`. It is only a warning. The program still builds and runs, and the variable will be used in the next version. - No semicolon trouble. Each
letand eachprintln!is a statement that ends with;.
Edge cases and common mistakes
- Dividing before multiplying.
salary * (15 / 100)printsBonus: $0for a salary of 135000. - Rounding. A salary of 135001 gives a bonus of 20250 and a salary of 6 gives 0, because the fraction is dropped rather than rounded to the nearest whole number.
- Big salaries. An
i32holds values up to 2,147,483,647, and the intermediate productsalary * 15must fit too. The lessons build with optimizations, where overflow wraps around silently instead of stopping the program. With a salary of 143165576 the bonus is 21474836, but with 143165577 the product no longer fits and the program prints a bonus of -21474836. A debug build would panic at the same point. The salary in this lesson is far below the limit. - Text instead of a number. If you wrote the salary as
"135000"in quotes, it would be text, and multiplying text by a number doesn't compile.
Testing
- A salary of 135000 gives
Bonus: $20250. - A salary of 100 gives
Bonus: $15. - A salary of 6 gives
Bonus: $0. - A salary of 135001 gives
Bonus: $20250.
