How to Write Functions in Rust

Define Rust functions using the fn keyword, specifying argument types and optional return types within curly braces.

You define a function in Rust using the fn keyword, followed by the function name, parentheses for arguments, and curly braces for the body.

fn main() {
    println!("Hello, world!");
}

fn add(x: i32, y: i32) -> i32 {
    x + y
}

The fn keyword starts the definition, main is the entry point, and add demonstrates parameters with types (i32) and a return type (-> i32).