Rullst AcademyRullst Academy/Lesson
Rust: from idea to program · Lesson 05 / 08

One function, one responsibility

The same scoring rule appears on several screens. We can write it once, give it a name and test its behavior.

Learning paths9 min

Let's discover together

  1. 1

    fn total(items: u32, each: u32) -> u32 defines a function named total. It receives two nonnegative integers and returns another. The names describe each input’s role.

  2. 2

    The body { items * each } multiplies the inputs. The final expression, without a semicolon, becomes the returned value. return also exists, but is unnecessary here.

  3. 3

    Calling total(3, 4) gives items the value 3 and each the value 4. The function returns 12. Returning is not the same as displaying: println! handles display.

  4. 4

    A small function makes normal and boundary cases easier to test. Our example uses small quantities; a real application would also need limits and handling for possible numeric overflow.

✦
YOUR MISSION

Read a function’s inputs and output and calculate three calls, including a zero input.

Now it's your turn

Discovery workshop

One rule, three calls

Function: fn total(items: u32, each: u32) -> u32 { items * each }. All values in this activity fit the type.
Build your answers and check when you are ready.
A moment to understand

Which body returns the multiplication as its final expression?

I need a hint

In this return form, the final expression does not end with ;.

Loading your journey…

Take it with you

Name a function that receives a quantity and returns double. Write two input/output examples before writing its body.

Show me the Rust behind it

This part is optional. You don't need to memorize the code to continue.

fn total(items: u32, each: u32) -> u32 {
    items * each
}

fn main() {
    println!("{}", total(3, 4));
}
At your own pace

My files and GitHub

Every lesson can become your own record: code, notes or a practice plan. Saving a file to GitHub is optional and does not change your XP.

Student guide

The editor and downloads open when this lesson is available to your account. Check access below or return to the track map.

Checking your access to this lesson…

Rullstpetbot

AI can make mistakes. Check the lesson material.