Problem overview
The program has two variables: price, the original price of a product in dollars, and discount, a percentage. It must print how many dollars the discount is worth, which is the discount amount, not the new price.
Take price = 150 and discount = 10. A 10% discount on 150 dollars is 15 dollars, so the answer is 15. In the editor the values are 70.0 and 15.0, and the answer is 10.5.
How to think about it
A percentage is a fraction of 100. Ten percent means 10 out of every 100, or 10 / 100 = 0.1 of the price. So the discount amount is the price times the percentage, divided by 100:
amount = price * discount / 100
150 * 10 / 100 = 1500 / 100 = 15
Multiplication and division share the same level and run from left to right, so price * discount / 100.0 first multiplies and then divides. The result would be the same with the division written first as price * (discount / 100.0) for these values, but multiplying first is the common choice, because it keeps whole numbers whole for as long as possible.
A second check makes the idea concrete: a 50% discount on 80 dollars should be half the price, and indeed 80 * 50 / 100 = 4000 / 100 = 40. A 100% discount takes off the whole price, and a 0% discount takes off nothing. Testing a formula against such easy cases before trusting it on awkward numbers is a habit worth building.
Step by step
- Multiply
pricebydiscount. For 70 and 15 that is 1050. - Divide by
100.0to turn the percentage into dollars: 1050 / 100 = 10.5. - Print the expression with
println!and the"{}"format string.
Why 100.0 and not 100
Both variables are declared as f64, Rust's 64-bit floating-point type, so a price like 70.5 or a discount like 12.5 is allowed. Rust never mixes floats and integers in arithmetic, so the divisor has to be a float too. Writing / 100 fails to compile:
error[E0277]: cannot divide `f64` by `{integer}`
A literal with a decimal point, such as 100.0, has type f64, which fixes the error. You don't need a cast or a suffix.
The solution in Rust
let price: f64 = 70.0;
let discount: f64 = 15.0;
println!("{}", price * discount / 100.0);
This prints 10.5. Details about how Rust prints floats:
- A float with no fractional part prints without a decimal point: with
price = 150.0anddiscount = 10.0the output is15, not15.0. The debug format{:?}would print15.0. - Floats are binary approximations, so some results show extra digits. With a price of
19.99and a discount of15.0the same expression prints2.9984999999999995. To show cents, use a precision in the placeholder:println!("{:.2}", ...)prints3.00.
Why integers would be a trap
If the variables were i32, the same formula with integer division would drop the fraction. With 70 and 15, 70 * 15 / 100 is 10, not 10.5. And the shortcut of dividing first, discount / 100, gives 0 for any whole discount below 100, so the whole result collapses to 0. Floats avoid both problems.
Reading the starter code
The two lines declare their types explicitly with : f64. That is optional here, because a literal like 70.0 already defaults to f64, but it documents the intent. Variables in Rust are immutable by default, so the program only reads price and discount and prints a new value, never modifying either.
Edge cases and common mistakes
- A zero discount prints
0, and a discount of 100 prints the whole price. - Printing the new price. This lesson asks only for the amount taken off. A related exercise subtracts it from the price.
- Forgetting the decimal point in the divisor, which is the compile error above.
- Treating the percentage as a fraction.
price * discountalone gives 1050, which is 100 times too large.
Testing
price = 150.0,discount = 10.0prints15.price = 70.0,discount = 15.0prints10.5.price = 80.0,discount = 0.0prints0.
