Start with the idea
Parameter types and the return type are always written out, never inferred across a function boundary. The last expression is the return value when you leave the semicolon off, which is why an accidental semicolon turns a function into one that returns the unit type and produces a type error.
Read the code
fn area(width: u32, height: u32) -> u32 {
width * height // no semicolon: the result
}
fn shout(text: &str) -> String {
let mut out = text.to_uppercase();
out.push('!');
out
}The final expression is the result — no return keyword needed.
Practice starter
fn double(value: i32) -> i32 {
value * 2;
}
Work through it
The area function declares its contract before its body: two u32 parameters go in and one u32 comes out. That boundary lets rustc check a call without reading every line of the implementation. Inside the body, width * height is the tail expression. Its value becomes the return value because there is no semicolon after it. Adding one would change that final statement to unit, and rustc would point out the mismatch with -> u32.
The shout function shows that a body can have several steps before its tail value. text is borrowed as &str, to_uppercase creates an owned String, and push modifies that owned buffer. The final out expression moves the completed String to the caller. The function does not return a reference to its local buffer, which would be invalid after the call. A signature is therefore also a useful hint about ownership: &str is borrowed input, String is an owned result.
When writing a new function, start with the inputs and result you want a caller to see. Then use a small example call to test that contract. If two branches can return different kinds of value, resolve that design rather than hoping inference will cross the function boundary. Predict the type of each tail expression, and compare it with the declared return type before compiling.
A function name should describe the guarantee it gives, while the signature makes the input and output types checkable. Keep the body focused enough that a reader can predict its result before scrolling. Test a normal value and an edge case; if the latter is ambiguous, specify the behavior in the contract.
A function gives one operation a name and a boundary. Parameters are inputs chosen by the caller; the declared return type tells callers what comes back. A function body returns its final expression unless it uses an explicit return. Printing inside a function and returning a value are different effects. A caller can use a returned number in another expression; printed text is already sent to stdout and is not the function's return value.
Call area(4, 3) and shout("hi"), then write down one row for each call: argument values, parameter bindings, and calculated result. Change a parameter type and see which call sites fail to compile. Then add a semicolon after width * height to witness the return-type error. Prefer a small function with a clear name over copying the same calculation in several places. If you cannot describe the function in one sentence, its responsibility may be too broad.
Try it
Return twice the input without printing it. Inputs are limited so doubling fits i32. In the Crabrix app, open the compiler lab and complete the supplied function in solution.rs. The reading question is a separate exercise.
What success looks like: The local compiler accepts the changed solution and the separate verification harness finishes all normal and boundary cases. Printing a success message alone does not complete this lab.
Check your understanding
Why does that function fail to compile?
Reveal the answer
- Multiplication needs both operands to be the same width
- The semicolon discards the value, so it returns ()
- The parameters must be declared mut to be read twice
A trailing semicolon turns an expression into a statement, and the function then returns ().
Learn this lesson with AI
Paste this into Claude, Codex, Cursor or another agent connected to Crabrix Academy. It contains only this lesson’s public details.
I'm learning Rust with Crabrix Academy. Teach me this lesson with the Crabrix Academy tools. Lesson: Functions (courseId "basics", lessonId "functions") Link: https://crabrix.com/learn/basics/functions/ Goal: The last expression without a semicolon is the return value. Practice: open the starter in the Crabrix Playground (https://crabrix.com/playground/?lesson=basics/functions) and I will write the code there. Explain it step by step for my level, let me write the code myself, give hints before any solution, and do not mark anything as passed: only Crabrix checks do that.