Pybites Logo Rust Platform

Functions and Return Values

Easy +2 pts

🎯 In Python, functions use def, type hints are optional, and you return with return:

def area(width: int, height: int) -> int:
    return width * height

Rust functions look similar but with important differences:

fn area(width: i32, height: i32) -> i32 {
    width * height
}

Three things to notice:

  1. Types are required β€” every parameter and the return type must be declared. No optional hints.
  2. Expression-based returns β€” the last expression (without a semicolon) is the return value. No return keyword needed.
  3. Semicolons matter β€” adding a semicolon to the last line turns it into a statement and the function returns () (Rust's unit type, like Python's None).

The semicolon trap

This is a common gotcha for Python developers:

fn broken(width: i32, height: i32) -> i32 {
    width * height;  // semicolon makes this a statement, not a return value
    // ERROR: expected i32, found ()
}

Remove the semicolon and it works. The return keyword exists but is mainly used for early returns from within branches.


Your Task

Implement add_and_double β€” takes two i32 values, returns (a + b) * 2.

Use expression-based return (no return keyword).


Example

assert_eq!(add_and_double(2, 3), 10);
assert_eq!(add_and_double(-1, 4), 6);

Further Reading