Rust · Chapter 4 of 26

Functions

By the end of this chapter you can write functions with typed parameters and a return type, return a value from the last expression or early with return, tell a statement from an expression (and spot the semicolon that turns one into the other), return two values at once in a tuple, document a function with ///, and split a program into files that main.rs uses with mod and pub. 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

The rule for what a litre is worth, fat * fat_paise + snf * snf_paise, is used by the collection box for every slip, by Neha's ten-day payout for every one of 1,01,640 pourings, and by the plant's service to check what each box sent. In chapters 1 to 3 every program wrote the rule out again where it needed it. Three copies are three places to change when the rate chart changes, and the night someone changes two of them, the box and the payout disagree by a few paise on every can, and Neha finds out from the farmers.

A function writes the rule once, gives it a name and typed inputs and an output, and lets every part of the program, and the Check, call it. From this chapter on, every task is a function, and the chapter's programs are built from files of them.

What you build: 4 tasks

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

  1. Task 1

    rate.rs: a litre's price, from a function

    Write pub fn per_litre(fat: u32, snf: u32, fat_paise: u32, snf_paise: u32) -> u64 in rate.rs. It takes a can's fat and SNF (in tenths of a percent) and the chart's fat paise and SNF paise, and returns what a litre is worth in paise: fat times fat_paise plus snf times snf_paise. Give it a doc comment, a line starting with /// right above pub fn per_litre, saying what it returns (the Check looks for it).

    The Check calls per_litre(64, 91, 40, 16) and expects 4016, per_litre(41, 84, 32, 20) 2992, per_litre(0, 0, 32, 20) 0, per_litre(100, 100, 40, 16) 5600, and per_litre(100_000, 100_000, 50_000, 50_000) 10_000_000_000. The last call's products do not fit in a u32, so each number must become a u64 before it is multiplied.

  2. Task 2

    amount.rs: what a can earns, to the nearest paisa

    Write pub fn amount(ml: u32, per_litre: u64) -> u64 in amount.rs. It takes a can's millilitres and its price in paise a litre, and returns what the can earns in paise: ml times per_litre divided by 1000, rounded to the nearest paisa with Neha's rule, so a half paisa rounds up. Use whole numbers only, with no f64 anywhere in the file (the Check looks).

    The Check calls amount(5250, 4016) and expects 21084, amount(4880, 2980) 14542 (14542.4 exactly), amount(3120, 3346) 10440 (10439.52 exactly), amount(1500, 3001) 4502 (4501.5 exactly), amount(0, 4016) 0, amount(16770, 4824) 80898, and amount(4_000_000, 4_000_000) 16_000_000_000.

  3. Task 3

    split.rs: rupees and paise, from one function

    Write pub fn split(paise: u64) -> (u64, u64) in split.rs. It takes an amount in paise and returns a tuple of two numbers: the whole rupees first, then the paise left over.

    The Check calls split(21084) and expects (210, 84), split(5) (0, 5), split(100) (1, 0), split(0) (0, 0), and split(293257264) (2932572, 64).

  4. Task 4 · interview question

    Interview: payout.rs, the function that returns nothing

    Lakhan wrote payout in payout.rs: what a farmer is paid for the day, the morning and evening amounts together, less the society's fee, all in paise. It compiles, but cargo run prints Paid for the day: () paise instead of 32434. Fix it so that it returns the pay as a u64: keep its name and its three u64 parameters, and make the signature pub fn payout(morning: u64, evening: u64, fee: u64) -> u64.

    The Check calls payout(21084, 11650, 300) and expects 32434, payout(0, 11650, 0) 11650, payout(500, 0, 0) 500, and payout(14542, 10440, 1000) 23982, and checks that it returns a u64.

    Farhan will ask: why did one semicolon change what the function returns?

What you will learn

  • fn: a function with typed parameters and a return type.
  • The last expression is the value, and return leaves early.
  • Statements and expressions: the semicolon that changes a type, and the unit type ().
  • Tuples: two or more values as one, taken apart with let (a, b).
  • Doc comments: /// above a function says what it does.
  • main.rs, mod and pub: a program made of several files.

More in The language

Build "Functions" 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.