Constants and Shadowing
Level: intro (score: 1)
In this exercise, you'll practice using Rust's constants and shadowing features by implementing a function that calculates the final price after applying tax and a discount.
Your function should:
- Take a base price as an f32
.
- Use two constants: one for the tax rate (8%
) and one for the discount percentage (10%
).
- First, shadow the base price by applying the tax. (Shadowing means you can reuse the same variable name to store a new value, effectively "overwriting" the previous one.)
- Then, shadow the result by applying the discount.
- Return the final price as an f32
.
Hint: Use the const
keyword for your constants. Shadowing lets you simplify your code by reusing variable names for transformed values.
Note: While shadowing is a useful Rust feature that allows reusing variable names for transformed values, it is not strictly required for this exercise. You could achieve the same result using a mut
variable instead. However, shadowing can help make intermediate transformations explicit and prevent unintended mutations elsewhere in a function.
Happy coding!