AnyCompiler

Functions

A function's parameters and return type are always annotated explicitly. The last expression in a function body — written without a trailing semicolon — becomes its return value; you rarely need an explicit return keyword.

fn add(a: i32, b: i32) -> i32 {
    a + b // no semicolon - this is the return value
}

fn main() {
    let sum = add(3, 4);
    println!("sum = {}", sum);
}
Output
sum = 7

Expressions vs. statements

This distinction runs through the whole language, not just function bodies: a block { ... } is itself an expression, and its value is whatever its last line evaluates to, as long as that line has no semicolon. Add a semicolon, and it becomes a statement instead, with no value.

fn main() {
    let y = {
        let x = 3;
        x + 1 // expression, no semicolon - becomes the block's value
    };
    println!("y = {}", y);
}
Output
y = 4

Next: control flow — if as an expression, and Rust's three loop forms.

Practice exercise

Write a function multiply(a: i32, b: i32) -> i32 that returns a * b, then print multiply(6, 7).