Start learning free

LESSON 57 OF 293 · CALCULATIONS - CHALLENGES

Employee Of The Year V1 in Rust: One Employee's Bonus

Practice Read the full explanation

Compute an end-of-year performance bonus for one employee, then print the employee's name and the bonus.

You start with:

    let name = "John Doe";
    let productivity = 83;
    let salary = 135000;

Build:

John Doe
Bonus: $20250

Rules:

  • The bonus is 15% of the salary, rounded down to a whole number.
  • Print the employee's name on the first line.
  • Print the bonus on the second line as "Bonus: $<amount>".
  • productivity is not used in this version. It is there for the next versions, where you will compare employees by productivity. (The compiler may warn that it is unused; that is fine.)

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

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

    let name = "John Doe";
    let productivity = 83;
    let salary = 135000;

    let bonus = salary * 15 / 100;
    println!("{}", name);
    println!("Bonus: ${}", bonus);

STEP 1 OF 3
    Ctrl + Enter to check

    RUST · CALCULATIONS - CHALLENGES

    Employee Of The Year V1 in Rust, explained

    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

    1. Compute the bonus. Create a variable named bonus and give it salary * 15 / 100. Both operators have the same precedence and work left to right, so the multiplication happens before the division.
    2. Print the name. The first output line is the employee's name on its own line.
    3. Print the bonus line. The second line is the text Bonus: $ followed by the number. A format string with a {} placeholder puts the value of bonus after 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 type i32. name is 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. productivity is never read in this version, so the compiler prints warning: 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 let and each println! is a statement that ends with ;.

    Edge cases and common mistakes

    • Dividing before multiplying. salary * (15 / 100) prints Bonus: $0 for 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 i32 holds values up to 2,147,483,647, and the intermediate product salary * 15 must 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.
    Keep going with Rust1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free