Tsallaka zuwa abun ciki
Justice Tech School

Rust Functions

Declare with fn. Parameters need types and the return type follows ->. The last expression without a semicolon is the return value.

Example
fn add(a: i32, b: i32) -> i32 {
    a + b
}
fn greet(name: &str) {
    println!("Hello, {}", name);
}
fn main() {
    println!("{}", add(2, 3));
    greet("Ada");
}

Result

5
Hello, Ada

Exercise

Return type arrow.

fn f()  i32 { 1 }

Quiz Test

How does a function return its last value?

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