Introduction
Programs become useful when they can choose between alternatives and repeat a calculation for several values. Rust uses Boolean conditions for decisions and loops for controlled repetition.
In this lab, you will complete four small parts of a prepared shipping summary. Each step changes only one focused expression: a comparison, two branch results, a running-total expression, and an acceptance condition.
Create a Boolean Comparison
In this step, you will compare a package weight with a limit and store the true-or-false result.
Enter the prepared project and open its source file:
cd /home/labex/project/shipping-summary
nano src/main.rs
The comparison operator == asks whether two values are equal. This is different from the single = used earlier to store or assign a value. The starter comparison is false because 12 is not equal to 10. For the report, a package is heavy when its weight is greater than the limit.
On the Step 1 line, replace only == with the greater-than operator >:
let is_heavy = package_weight > heavy_limit; // Step 1
The comparison produces the Boolean value true, which is stored in is_heavy. Save with Ctrl+O, press Enter, and exit with Ctrl+X. Run the program:
cargo run --quiet
Its first line should now be:
Heavy package: true
Choose a Value with an If Expression
In this step, you will use an if expression to choose the shipping label.
An if checks a Boolean condition. When the condition is true, Rust evaluates the first block; otherwise it evaluates the else block. In Rust, if can be an expression that produces a value. Both branches here produce text, so the selected text can be assigned to shipping_label. Because the entire if becomes one stored value, its branches must produce compatible types.
Open the file:
nano src/main.rs
On the Step 2 line, replace the first "unknown" with "Freight" and the second with "Standard":
let shipping_label = if is_heavy { "Freight" } else { "Standard" }; // Step 2
Rust does not require parentheses around the condition. The braces clearly separate the two possible results.
Save, exit, and run the report:
cargo run --quiet
Because is_heavy is true, the second line should be:
Shipping label: Freight
Add Values with a For Loop
In this step, you will use a bounded for loop and an inclusive range to build a running total.
The range 3..=6 represents the integers from 3 through 6, including both endpoints. A for loop takes each value from that range in order and temporarily names it weight inside the loop body.
The mutable total_weight starts at zero. Each repetition should add the current weight to the existing total.
Open the file and find the Step 3 line:
nano src/main.rs
Replace only weight * 0 with weight:
total_weight = total_weight + weight; // Step 3
Save and exit, then check the code:
cargo check
Run the report:
cargo run --quiet
The loop adds 3 + 4 + 5 + 6, so you should see:
Total weight: 18
Make a Decision Inside a Loop
In this step, you will combine repetition with a decision to count accepted orders.
The second loop also visits weights 3 through 6. An order is accepted when its weight is less than or equal to 5. The operator <= means less than or equal to.
Open the file and find the Step 4 line:
nano src/main.rs
Replace the temporary comparison weight < 0 with:
let is_accepted = weight <= 5; // Step 4
The following if is used as a statement: when is_accepted is true, its block increases the counter. There is no else because rejected orders require no action here.
Save and exit, then run the completed report:
cargo run --quiet
Weights 3, 4, and 5 are accepted, while 6 is not. The final output should be:
Heavy package: true
Shipping label: Freight
Total weight: 18
Accepted orders: 3
Summary
You created Boolean comparisons, selected a value with an if expression, repeated work with a for loop and inclusive range, maintained a running total, and placed a decision inside a loop to count matching values.



