Rust functions

From wikinotes
Revision as of 00:54, 7 February 2023 by Will (talk | contribs)

Expressions vs Statements

  • statements include actions without a return value (ends in ;)
  • expressions include actions with a return value (no ;)

statement

{
    let y = 1;
    y += 1;
} // no return val

expression

let x = {
    let y = 1;
    y += 1       // <-- no semicolon
} // returns 2

Function Signatures

fn main() {            // returns void
    println!("hi");
}

fn main() i32 {        // returns 123
    123  // <-- no semicolon
}

fn main(num: u8) {     // param type
    println!("{num}");
}