Functions
A function in Rust is declared with the fn keyword: a name in snake_case, parameters in parentheses, an optional result type after ->, and a body in curly braces. A program starts at the main function, and the other functions can be declared in any order: the compiler reads the whole file, and forward declarations such as C prototypes are not needed.
The syntax is simple, but two rules sit underneath it, and those are what the junior section checks. First: every parameter's type is written explicitly — type inference works inside a function body but never in its signature. On a call, the argument is copied into the parameter if it is a simple number, or moved if it is a String, and then the calling code can no longer use it. Second: Rust is an expression language. A { } block has a value, and a function's result is its last expression without a semicolon. One extra ; turns the result into the empty tuple () and breaks the build with error[E0308]: mismatched types. The return keyword stays for early exit, and functions that never return at all get the special ! type. This topic walks functions layer by layer — from the declaration to the return values.
Topic map
- Function Basics —
fn, asnake_casename, parameters and a body; definition order does not matter;mainis the entry point; a nestedfndoes not see the outer function's local variables. - Parameters — every parameter needs a type; the argument is copied or moved into the parameter;
muton a parameter changes only its local value; there is no overloading and no default values. - Expressions and Statements — an expression yields a value, a statement does not;
letis a statement; a{ }block andifare expressions;;turns an expression into a statement. - Return Values —
-> Typeand the last expression without;; the extra-;trap;returnfor early exit; without->a function returns(); the!type; amainthat returnsResult.
Common mistakes and traps
| Mistake | Consequence |
|---|---|
Putting ; after the last expression of a function with -> i32 | error[E0308]: mismatched types: the body returns () instead of i32 |
| Leaving out a parameter's type and hoping for type inference | Does not compile: inference works in the body, not in the signature |
Using a String after passing it to a function | error[E0382]: borrow of moved value: `s` : the string was moved into the parameter |
Expecting mut n: u32 on a parameter to change the caller's variable | The function's local value changes; changing the caller's variable takes a mutable reference &mut |
Writing return x; on the last line of every function | Works, but is not idiomatic: return is for early exit |
Trying to write let x = (let y = 6); | error: expected expression, found `let` statement: let has no value |
| Naming functions in camelCase | The compiler emits a non_snake_case warning |
Why it matters in interviews
Functions are the first topic where Rust visibly parts ways with familiar languages: the answer to "what does this function return?" depends on a single semicolon. What gets checked here is not syntax but an understanding of the difference between an expression and a statement.
What interviewers usually check:
- How an expression differs from a statement, and why an extra
;at the end of a function giveserror[E0308]: mismatched types. - Why a parameter's type cannot be omitted even though types are inferred inside the body.
- What happens to a
Stringafter it is passed to a function, and how that differs from passing ani32. - When
returnis needed and when the last expression is enough. - What
()and!are, and which of them plays the role ofvoid. - Whether
maincan return aResult, and what happens on an error then.