liveBook · Manning

Function

As explained in Learn Rust in a Month of Lunches

A function is a named unit of executable code that can accept inputs and produce a result.

A function is a named unit of executable code. It can receive inputs, perform work, and return a result. In Rust, `fn` declares a function, `()` shows its arguments, and `{}` contains its code block. section 1.6

Rust functions make several parts of a program explicit:

  • An empty `()` means the function accepts no arguments; values inside the parentheses are its inputs. section 1.6
  • A return arrow such as `-> i32` declares the result type. section 1.6
  • A final expression without a semicolon becomes the return value; adding a semicolon produces the unit type `()` instead. section 1.6
  • Parameters can be typed and used inside the body, allowing one function to operate on different supplied values. section 1.6

Returning a calculated value

fn multiply(number_one: i32, number_two: i32) -> i32 { number_one * number_two } fn main() { let multiply_result = multiply(8, 9); println!("The two numbers multiplied are: {}", multiply_result); } The function receives two `i32` values and returns their product. The last line has no semicolon, so it supplies the result. section 1.6

fn multiply(number_one: i32, number_two: i32) {   #1
    let result = number_one * number_two;
    println!("{} times {} is {}", number_one, number_two, result);
}
 
fn main() {
    multiply(8, 9);                               #2
    let some_number = 10;                         #3
    let some_other_number = 2;
    multiply(some_number, some_other_number);     #4
}

Browse a complete function call with typed parameters, local variables, and two sets of arguments. section 1.6

Compiler feedback helps locate return mistakes: if a function promises `i32` but ends with `8;`, Rust reports that it implicitly returns `()` and suggests removing the semicolon. A function may also use `return`, though code after it is unreachable. section 1.6

Small helper functions can improve readability by moving repeated or overly long work out of a main function. Keep important information visible, because chains of helpers can make code harder to follow. section 23.4.3

How Learn Rust in a Month of Lunches covers it

How deep it goes
Defines syntax, arguments, return types, semicolon behavior, compiler feedback, and helper-function readability, but does not develop advanced function internals.
Examples
Uses `main`, `give_number`, typed multiplication, compiler diagnostics, helper functions, and asynchronous number-returning functions.
What it assumes
Teaches function terminology from zero, including arguments, code blocks, lifetimes, return arrows, and the unit type.
What you can do after
Build it from scratch by declaring Rust functions, supplying typed arguments, returning values, and correcting semicolon-related compiler errors.
How it connects
Connects functions with arguments, code blocks, variables, return values, helper functions, and async functions.

Also explained in

Compare how 20 books explain function →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn Rust in a Month of Lunches. The section links point to the parts of the book it draws on.