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:
subtotalis 12 * 3 = 36.after_discountis 36 - 5 = 31.taxis 31 * 8 / 100. The product 248 divided by 100 is 2, and the remaining 48 is dropped.totalis 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
- Declare the data:
item,price,quantity,tax_rateanddiscount, each withlet. - Compute
subtotalas the price times the quantity. - Compute
after_discountas the subtotal minus the discount. The discount is a flat amount taken off before tax. - Compute
taxby multiplyingafter_discountbytax_rateand dividing by 100. Between two integers,/drops the fraction, so the tax stays a whole number. - Compute
totalasafter_discount + tax. - Build the four rows
line1toline4withformat!, each a label with its dollar sign followed by a placeholder: Subtotal, Discount, Tax and Total. - Join the rows into
resultwith a newline between each two. Four rows need three newlines. - Print
resultwithprintln!.
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 twoi32values staysi32. 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 thatitemis unused. It is a warning and doesn't stop the build. - Four placeholders, four arguments. The template
"{}\n{}\n{}\n{}"has four{}, soformat!needs four values after it. Fewer is a compile error.
Edge cases and common mistakes
- Dividing first. Computing
tax_rate / 100 * after_discountgives 0, because8 / 100is 0 in integers. The receipt would showTax: $0andTotal: $31. - Taxing the subtotal.
subtotal * tax_rate / 100is 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_discountis -2, the tax is 0 and the receipt endsTotal: $-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.
