Skip to content
Justice Tech School

Functions

Functions use fn, typed parameters and an arrow for the return type. The last expression without a semicolon is returned.

Example
fn add(a: i32, b: i32) -> i32 { a + b }
fn square(n: i32) -> i32 { return n * n; }

fn main() {
    println!("{} {}", add(1, 2), square(5));
}

Result

3 25

Exercise

Return type arrow.

fn f()  i32

Quiz Test

How do you return a value?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.