Start learning free

LESSON 193 OF 293 · PROJECTS: EVERYDAY PROGRAMS

Shopping Cart V2 in Rust: Tax and Discount with Integer Math

Practice Read the full explanation

Add a discount and sales tax to a single-item purchase using integer math.

You start with: an empty editor (no starter code). Declare this data yourself:

let item = "Coffee Mug";
let price = 12;
let quantity = 3;
let tax_rate = 8;
let discount = 5;

Build:

Subtotal: $36
Discount: $5
Tax: $2
Total: $33

Rules:

  • subtotal = price * quantity.
  • after_discount = subtotal - discount (the discount is a flat amount taken off before tax).
  • tax = after_discount * tax_rate / 100 (integer division, so the result is rounded down). The tax is charged on the discounted amount.
  • total = after_discount + tax.
  • Build four lines (Subtotal, Discount, Tax, Total) and join them with a newline "\n" into one string result, then print it with println!("{}", result).

Compute the four numbers (subtotal, after_discount, tax, total) before formatting.

Use format!("Total: ${}", total) to build each line from a number.

Rust's i32 / i32 truncates, so after_discount * tax_rate / 100 already rounds down.

        let item = "Coffee Mug";
        let price = 12;
        let quantity = 3;
        let tax_rate = 8;
        let discount = 5;
        let subtotal = price * quantity;
        let after_discount = subtotal - discount;
        let tax = after_discount * tax_rate / 100;
        let total = after_discount + tax;
        let line1 = format!("Subtotal: ${}", subtotal);
        let line2 = format!("Discount: ${}", discount);
        let line3 = format!("Tax: ${}", tax);
        let line4 = format!("Total: ${}", total);
        let result = format!("{}\n{}\n{}\n{}", line1, line2, line3, line4);
        println!("{}", result);

STEP 1 OF 15
    Ctrl + Enter to check

    RUST · PROJECTS: EVERYDAY PROGRAMS

    Shopping Cart V2 in Rust, explained

    Problem overview

    Version 2 of the Shopping Cart project adds a discount and sales tax to the single-item purchase. You declare the data yourself: the item Coffee Mug, a price of 12, a quantity of 3, a tax rate of 8 (percent) and a flat discount of 5 dollars. The program prints a four-line receipt:

    Subtotal: $36
    Discount: $5
    Tax: $2
    Total: $33
    

    The order of the calculation is fixed by the rules. The subtotal is price times quantity. The discount comes off first, giving after_discount. The tax is charged on that discounted amount, as after_discount * tax_rate / 100, using integer division so the result is rounded down. The total is the discounted amount plus the tax.

    The previous version printed an item line and a total. The new work is the extra steps between the subtotal and the total.

    Approach: one variable per rule

    Each rule becomes one variable, in the order the rules are stated, so every line of code can be checked against one sentence of the specification. With the lesson's numbers:

    • subtotal is 12 * 3 = 36.
    • after_discount is 36 - 5 = 31.
    • tax is 31 * 8 / 100. The product 248 divided by 100 is 2, and the remaining 48 is dropped.
    • total is 31 + 2 = 33.

    The tax multiplies before it divides. Percentages with integers only work in that order, because dividing first would throw away the part that matters.

    Step by step

    1. Declare the data: item, price, quantity, tax_rate and discount, each with let.
    2. Compute subtotal as the price times the quantity.
    3. Compute after_discount as the subtotal minus the discount. The discount is a flat amount taken off before tax.
    4. Compute tax by multiplying after_discount by tax_rate and dividing by 100. Between two integers, / drops the fraction, so the tax stays a whole number.
    5. Compute total as after_discount + tax.
    6. Build the four rows line1 to line4 with format!, each a label with its dollar sign followed by a placeholder: Subtotal, Discount, Tax and Total.
    7. Join the rows into result with a newline between each two. Four rows need three newlines.
    8. Print result with println!.

    The solution in Rust

            let item = "Coffee Mug";
            let price = 12;
            let quantity = 3;
            let tax_rate = 8;
            let discount = 5;
            let subtotal = price * quantity;
            let after_discount = subtotal - discount;
            let tax = after_discount * tax_rate / 100;
            let total = after_discount + tax;
            let line1 = format!("Subtotal: ${}", subtotal);
            let line2 = format!("Discount: ${}", discount);
            let line3 = format!("Tax: ${}", tax);
            let line4 = format!("Total: ${}", total);
            let result = format!("{}\n{}\n{}\n{}", line1, line2, line3, line4);
            println!("{}", result);
    

    Rust details worth knowing:

    • All the numbers are i32. Rust infers that type for whole-number literals, and arithmetic between two i32 values stays i32. The language never turns an integer into a decimal for you, so / between integers is always integer division.
    • Integer division truncates toward zero. For positive amounts that means rounding down. Rust has no automatic rounding to the nearest cent here.
    • Unused item. The item name isn't printed in this receipt, so the compiler warns that item is unused. It is a warning and doesn't stop the build.
    • Four placeholders, four arguments. The template "{}\n{}\n{}\n{}" has four {}, so format! needs four values after it. Fewer is a compile error.

    Edge cases and common mistakes

    • Dividing first. Computing tax_rate / 100 * after_discount gives 0, because 8 / 100 is 0 in integers. The receipt would show Tax: $0 and Total: $31.
    • Taxing the subtotal. subtotal * tax_rate / 100 is 36 * 8 / 100 = 2 here as well, so the lesson's data can't tell the formulas apart. With other numbers they differ: take a price of 10, a quantity of 1, a rate of 10 and a discount of 5. Tax on the discounted amount is 0 (5 * 10 / 100 rounds down), while tax on the subtotal would be 1.
    • Rounding down. A price of 1 and a rate of 8 give a tax of 0, since 8 / 100 is below 1. The total is 1.
    • A discount larger than the subtotal. With a subtotal of 3 and a discount of 5, after_discount is -2, the tax is 0 and the receipt ends Total: $-2. The lesson doesn't ask you to guard against this, but it shows what the formulas do.
    • Row order and wording. The rows must be Subtotal, Discount, Tax and Total in that order, with the exact labels.
    • Missing newline. With only two newlines in the template, two rows would merge on one line.

    Testing

    These inputs show the receipt as Subtotal / Discount / Tax / Total amounts:

    • Price 12, quantity 3, rate 8, discount 5 gives 36, 5, 2 and 33.
    • Price 10, quantity 1, rate 10, discount 0 gives 10, 0, 1 and 11.
    • Price 10, quantity 1, rate 10, discount 5 gives 10, 5, 0 and 5.
    • Price 1, quantity 1, rate 8, discount 0 gives 1, 0, 0 and 1.
    • Price 100, quantity 2, rate 0, discount 0 gives 200, 0, 0 and 200.
    Keep going with Rust1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free