Return Values and Expressions

Rust functions can return values. The last expression (without a semicolon) is the return value.

Basic Return

fn add(a: i32, b: i32) -> i32 {
    a + b // No semicolon means it's returned
}

Use return explicitly if needed:

fn square(x: i32) -> i32 {
    return x * x;
}
← PrevNext →