wiki/dev/lang/rust/Function.md

556 B

Functions.

Declare.

Declare functions using fn keyword.

// Simple function.
fn main() {}

// With argument of type i32.
fn main(x: i32) {}

// With return type of i32.
fn main() -> i32 {}

Return value.

Return keyword.

You can return value by writing return keyword. Use this in multiline functions.

fn foo() -> i32
{
  return 10;
}

Last expression.

It is also possible to return the result of the last line by removing ; at the end. Use this for single-line functions.

fn foo() -> i32 { 10 }