Rust · Chapter 3 of 26

Control flow

By the end of this chapter you can make a program choose with if and else, use if as a value, repeat work with loop, while and for, stop a loop with a value, keep a fixed list of numbers in an array, and choose between many cases with match, knowing why the compiler insists that every case is covered. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: The language
  • 4 tasks
  • Rust
  • In the all labs plan

The first 2 chapters of Rust are free. This one comes with the all labs plan.

Why you need this

At Karamsad on Tuesday morning, 211 cans were poured: 205 were accepted and 6 rejected. A can is accepted only if its fat and SNF are at least the chart's minimums, so the box must choose: accepted or rejected, and a class for the fat. The 205 accepted cans brought 1,931 litres into the society's chilling tank, which holds 2,000; the box must repeat the same work for every can and stop at the moment the tank is full. And Lakhan keeps seven numbers for a week of milk and wants the total, the best day and the days below target.

Every program so far ran from its first line to its last, each line once. Without a way to choose, it cannot say "rejected" for one can and "accepted" for another; without a way to repeat, 211 cans need 211 copies of the same lines, and seven days need seven variables with seven names.

What you build: 4 tasks

Each task has tests. You write the code in the lab's editor and press Check.

  1. Task 1

    class.rs: a can's fat class

    Neha sorts cans into classes by their fat, in tenths of a percent, kept in the line let fat: u32 = 64;. Choose the class with match on ranges of fat (the Check looks for match): 0 to 29 is R, 30 to 44 is C, 45 to 59 is B, 60 to 69 is A, and 70 and up is A+. Then print exactly one line, with the fat as a percent with one digit after the point:

    Fat 6.4%: class A

    Keep the let line as it is: the Check also runs class.rs with fat 0, 29, 30, 44, 45, 59, 60, 69, 70 and 85 in it. Fat 0 prints Fat 0.0%: class R, fat 29 prints Fat 2.9%: class R, fat 30 prints Fat 3.0%: class C, fat 70 prints Fat 7.0%: class A+, and so on: every number lands in exactly one class.

  2. Task 2

    week.rs: a week of litres

    Lakhan keeps the litres Napad's society collected on each day of a week in an array, day 1 first, in the line let litres = [1098, 1125, 1141, 1087, 1160, 1152, 1119];, and a target in let target: u32 = 1120;. Work out from them, and print exactly:

    Total: 7882 litres

    Best day: day 5, 1160 litres

    Days below 1120 litres: 3

    The total is the sum of the seven days. The best day is the day with the most litres, numbered from 1; if two days tie, the earlier one. A day below the target has fewer litres than it (a day equal to the target is not below).

    Keep the two let lines as they are: the Check also runs week.rs with [900, 950, 950, 800, 950, 700, 600] and target 1000 (Total: 5850 litres, Best day: day 2, 950 litres, Days below 1000 litres: 7), with [1200, 1210, 1190, 1250, 1250, 1300, 1300] and target 1190 (Total: 8700 litres, Best day: day 6, 1300 litres, Days below 1190 litres: 0), and with [1500, 1000, 1000, 1000, 1000, 1000, 1000] and target 1000 (Total: 7500 litres, Best day: day 1, 1500 litres, Days below 1000 litres: 0).

  3. Task 3

    tank.rs: filling the chilling tank

    Karamsad's chilling tank holds 2,000 litres, the constant TANK_ML (2000000 ml). It already has the ml in let mut in_tank: u32 = 1_950_000;, and the cans in the array let cans = [12580, 5160, 12460, 6980, 2650, 6800, 4150]; are poured in one by one, in order. Use loop (the Check looks for loop and break): pour the next can, print a line for it, and stop with break as soon as the tank holds TANK_ML or more, giving the number of the can that filled it (cans are numbered from 1) as the loop's value. If every can is poured and the tank is still not full, the loop must stop too, without reading past the last can. Print exactly:

    Can 1: 1962580 ml in the tank

    Can 2: 1967740 ml in the tank

    Can 3: 1980200 ml in the tank

    Can 4: 1987180 ml in the tank

    Can 5: 1989830 ml in the tank

    Can 6: 1996630 ml in the tank

    Can 7: 2000780 ml in the tank

    The tank was filled by can 7

    When the cans run out first, the last line instead gives the ml in the tank, such as The tank is not full: 1950780 ml.

    Keep the in_tank line as it is: the Check also runs tank.rs with 1_987_420 in it (Can 1: 2000000 ml in the tank, then The tank was filled by can 1: exactly full counts as full) and with 1_900_000 (seven can lines, the last Can 7: 1950780 ml in the tank, then The tank is not full: 1950780 ml).

  4. Task 4 · interview question

    Interview: status.rs, the if that has no else

    Lakhan wrote status.rs to say whether a cow can is accepted (fat at least 30 and SNF at least 80, in tenths) and what a litre of it pays on the cow chart (fat times 32 plus SNF times 20 paise), and it does not compile. Fix it so that it prints exactly:

    Status: accepted

    Pays: 2728 paise a litre

    A rejected can prints Status: rejected and Pays: 0 paise a litre.

    Keep the lines let fat: u32 = 34; and let snf: u32 = 82; as they are: the Check also runs status.rs with fat 28 and snf 85 (rejected, 0), fat 30 and snf 80 (accepted, 2560), fat 45 and snf 79 (rejected, 0), and fat 52 and snf 88 (accepted, 3424).

    Farhan will ask: Rust has no ternary operator, so how does it choose between two values, and why can an if with no else not give a value?

What you will learn

  • if and else: choose, with conditions joined by &&, || and !.
  • if as a value: let x = if .. { .. } else { .. };, Rust's answer to the ternary operator.
  • loop, while and for: repeat; break with a value, continue, and ranges like 1..=7.
  • Arrays: a fixed number of values of one type, [u32; 7].
  • match: choose between many cases, with ranges and |, every case covered.

More in The language

Build "Control flow" yourself

DoodhSetu runs the milk collection boxes of 120 village societies around Anand. The old box program crashed on an empty line, and you rebuild it in Rust. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.

The first 2 chapters of Rust are free. This one comes with the all labs plan.