LearningRust.org
LessonsPlaygroundAbout
Sign In
Lessons/basics/Functions in Rust
PreviousPracticeNext

TL;DR

Learn how to define Rust functions with parameters, return types, and expressions — the building blocks of every Rust program

Key concepts

  • Rust functions
  • Rust return types
  • Rust fn keyword
  • Rust function parameters
  • Rust expressions
Loading...

Next lesson

Closures

Rust closures tutorial — learn how closures capture variables, understand Fn, FnMut, and FnOnce traits, and use closures with iterators

25 min

Related lessons

  • Introduction to RustLearn Rust from scratch — discover why Rust is popular, set up your environment, and write your first program as a beginner
  • Variables and Data TypesMaster Rust variables, mutability, and data types including integers, floats, booleans, characters, and type annotations
  • Modules and CratesLearn how to organize Rust code with modules and manage dependencies using crates and Cargo

Also learn

ZigLearn Zig ProgrammingTypeScriptMaster TypeScript

Also learn

ZigLearn Zig ProgrammingTypeScriptMaster TypeScript

A14A

Building digital products that matter.

© 2026 A14A. All rights reserved.
KVK: 87105004PrivacyTerms

Functions in Rust

Functions are the building blocks of readable, maintainable code. In Rust, functions are declared using the fn keyword and follow a clear, consistent syntax.

Basic Function Syntax

Here's how you define a simple function in Rust:

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

To call this function, you simply write greet();

Functions with Parameters

Functions can take parameters, which must have type annotations:

fn greet_person(name: &str) {
    println!("Hello, {}!", name);
}

Return Values

Functions can return values. The return type is declared after an arrow ->:

fn add(x: i32, y: i32) -> i32 {
    x + y  // Note: no semicolon here!
}

Multiple Parameters

You can have multiple parameters with different types:

fn calculate_rectangle_area(width: f64, height: f64) -> f64 {
    width * height
}

Practice Time!

Try writing a function that:

  1. Takes two numbers as parameters
  2. Returns their sum
  3. Prints the result
fn main() {
    let result = add_numbers(5, 3);
    println!("The sum is: {}", result);
}

fn add_numbers(a: i32, b: i32) -> i32 {
    a + b
}

Key Points to Remember

  • Functions start with fn
  • Parameter types must be declared
  • Return types are declared with ->
  • The last expression in a function is implicitly returned
  • Early returns use the return keyword
  • Function names use snake_case by convention

Now it's your turn to experiment with functions in the playground below!

Key Takeaways

  • Functions are declared with fn and can return values using -> syntax
  • Rust functions must declare parameter types explicitly
  • The last expression in a function body is implicitly returned (no return needed)
  • Functions can return tuples to return multiple values
  • Closures capture variables from their environment

Next Steps

Now that you can write functions, you're ready to learn about closures — anonymous functions that can capture variables from their environment. Closures are used everywhere in Rust, from iterators to error handling.

Ready to continue? Head to Closures!