Problem overview
A student took three tests and scored 85, 92 and 78. The program stores the scores in variables named test1, test2 and test3, computes their average as a whole number and prints it as Average: 85.
The three scores add up to 255, and 255 divided by 3 is exactly 85. The statement asks for integer division, so for scores that don't divide evenly the program keeps the whole part and drops the rest. Scores of 85, 90 and 78 add up to 253, and the program prints 84, not 84.33.
The editor starts empty, so declaring the three scores is part of the work. This is the first version of a small report-card program that grows over the next lessons.
Approach: add, then divide
An average is the sum of the values divided by how many there are. With three tests, that is (test1 + test2 + test3) / 3. The parentheses matter: division binds tighter than addition, so without them only test3 would be divided.
All the scores are i32 values, and dividing an i32 by an i32 gives an i32. Rust truncates toward zero, so the result is already the whole number the lesson wants. There is no rounding function to call.
Step by step
- Declare
test1withletand the typei32, set to 85. - Declare
test2the same way, set to 92. - Declare
test3the same way, set to 78. - Compute
averageby adding the three scores in parentheses and dividing by 3. The sum has to be complete before the division happens. - Print the result with
println!. The{}in the text is replaced by the value after the comma.
The solution in Rust
let test1: i32 = 85;
let test2: i32 = 92;
let test3: i32 = 78;
let average: i32 = (test1 + test2 + test3) / 3;
println!("Average: {}", average);
Rust details worth knowing:
- Variables are immutable by default.
letbinds a value that can't be reassigned. None of these four variables changes after it is created, so none needsmut. - The type annotation is optional here.
let test1 = 85;would also give ani32, because that is the default type for an integer literal when nothing else says otherwise. Writing: i32makes the type visible. - Integer division truncates toward zero.
-7 / 2is-3. With test scores that are never negative, that is the same as rounding down. {}placeholders.println!is a macro, which is why it ends in!. Each{}takes the next argument and prints it in its default form.
Edge cases and common mistakes
- Forgetting the parentheses.
test1 + test2 + test3 / 3adds 85 and 92 to 78 / 3, which is 26, and prints 203. - Expecting a decimal. Dividing integers never produces a fraction. With scores of 85, 90 and 78 the program prints 84. A floating-point average would give
84.33333333333333, but that needsf64values and is not what this lesson asks for. - Dividing each score first. With three scores of 1,
1 / 3 + 1 / 3 + 1 / 3is 0, because every division drops its remainder before the addition. Dividing the sum gives 1. - A missing space in the text. The output must read
Average: 85, so the space after the colon belongs inside the quotes.
Testing
- Scores 85, 92 and 78 print
Average: 85. - Scores 85, 90 and 78 give 84.
- Scores 100, 100 and 99 give 99.
- Scores 1, 1 and 1 give 1.
