Coding/Learn/rust/functions and expressions
Rust Foundational Course
01.Introduction to Rust & Memory Safety
02.Formatting, Comments & Macros (println!)
03.Variables, Mutability & Shadowing
04.Scalar & Compound Types (Integers, Floats, Tuples, Arrays)
05.Functions, Parameters & Return Expressions
06.Conditionals & If as an Expression
07.Ownership Rules & Move Semantics
08.References & Borrowing (&T)
09.Mutable References (&mut) & Aliasing XOR Mutability
10.Slices & String Types (&str vs String)
11.Loops in Rust (loop, while, for)
12.Pattern Matching with match Expressions
13.Structs, Associated Functions & Methods (impl)
14.Enums & the Option<T> Type (No Null!)
15.Concise Pattern Matching (if let & let else)
16.Vectors & Growable Collections (Vec<T>)
17.Error Handling with Result<T, E> & the ? Operator
18.Traits, Shared Behavior & Generics
19.Closures & Functional Iterators (map, filter, collect)
20.Capstone: High-Performance In-Memory Log Telemetry Engine
Course Certificate0%

Complete all lessons to earn

LearnFunctions, Parameters & Return Expressions
5 / 20 0%
Lesson 05 of 20
6 mins read

Functions, Parameters & Return Expressions

PreviousNext Lesson
In Plain English

Functions in Rust are declared with fn. Parameters must have explicit type annotations. Rust is an expression-oriented language: blocks evaluate to their final expression, meaning omitting the trailing semicolon acts as an implicit return.

The Mental Model

"In Rust, a function body is like an assembly line: steps ending in semicolons perform side effects, but the final un-punched part at the end of the belt is the finished product handed right back to the caller."

Deep Dive: How It Works

Statements vs Expressions: Statements perform an action and return () (e.g. let x = 5;). Expressions evaluate to a resulting value (e.g. 5 + 5).

Implicit Return: Omit the semicolon on the final line of a function to return that value.

Explicit Return: The return keyword is available for early exits from function bodies.

Syntax Blueprint

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

Notice no semicolon after a + b—this marks it as the returned expression.

PointCore Rules to Remember

Point star
Explicit Signatures: Functions declare parameter types and return type (-> Type).
Point star
Expression Returns: The last expression without a semicolon is the return value.
Point star
Unit Type (): Functions that return nothing return the empty tuple () implicitly.

Common Beginner Traps & How to Fix Them

Mistake:Adding an accidental semicolon to the return expression.

Why it happens: fn add(a: i32, b: i32) -> i32 { a + b; } returns () instead of i32, causing mismatched types error.

How to fix: Remove the trailing semicolon from the return expression.

Pro Tip: Use block expressions to compute values inline: let total = { let sub = 100; sub + 15 };.

Live Interactive Example

Hit Run Code to see it live
Expression-Based Functions
Rust 1.78 / rustc
1
2
3
4
5
6
7
8
9
10
11
12
Output Console
Click "Run Code" to view the rendered output.
How it works: Demonstrates typed parameters, implicit expression return, and formatted printing.

Your Turn: Micro Challenge

No pressure! Edit the starter code below and test your solution with instant feedback.

Micro Exercise

Write a multiply function

  • PointWrite fn multiply(x: i32, y: i32) -> i32 that returns x * y without a semicolon.
  • PointIn main(), call multiply(7, 8) and print "Product: 56".
1
2
3
4
5
6
7
Sandbox Output
Click "Run & Check" to test your solution.

Finished reading and practicing?

Mark this lesson as completed to update your course progress.

PreviousNext Lesson
Overview100 SheetLearnCertificates