Functions
A function is a named piece of code with typed parameters and a typed return value. Rust functions do not nest overloads; each fn name in a module is unique.
Detailed Explanation
fn add(a: i32, b: i32) -> i32 { a + b } is a complete Rust function. The parameter types are mandatory. The return type is mandatory unless it is (), the unit type, in which case you may omit -> (). The last expression in the body is the return value if it has no semicolon. return exists and exits early; it is not how you return from the happy path.
There is no function overloading. You cannot write two fn parse functions that differ only by argument type. The usual replacements are different names (parse_port, parse_host), a generic function, or a trait method so that T::parse is selected by the type. Default arguments do not exist either. Optional configuration is an Option parameter, a builder, or a second function.
Ownership is part of the signature. fn take(s: String) consumes s. fn peek(s: &String) borrows it. fn edit(s: &mut String) borrows it mutably. Callers read the signature and know whether they can use the value afterwards. This is why Rust documentation leans so hard on the fn line: it is the contract, not a sketch.
Functions can be generic, fn first<T>(items: &[T]) -> Option<&T>, and they can take impl Trait in argument position for "some type that implements this trait" without naming a type parameter. They can be async, in which case the body returns a Future rather than running immediately. They can be const, which means they may run at compile time. They can be unsafe, which means the caller must uphold extra invariants the compiler cannot check.
Nested functions are allowed and are ordinary fn items that cannot capture their environment. If you need to capture, you want a closure. Methods are functions whose first parameter is a receiver and which live in an impl block; they are still functions, and Type::method(receiver, args) is the desugared call.
What it costs: each function is a compilation unit for monomorphisation when it is generic, so a generic fn used with twenty types is compiled twenty times. Non-generic functions have no such cost. Inlining is the compiler's decision; #[inline] is a hint, not a command.
When you write a function: any named operation with a clear input and output. Keep the signature honest about ownership. If the function needs three related parameters, those parameters probably want to be a struct.
Code Examples
fn add(a: i32, b: i32) -> i32 {
a + b // no semicolon: this is the return value
}
fn take_name(name: String) {
println!("hello {name}");
}
fn main() {
println!("{}", add(2, 40)); // prints: 42
let name = String::from("Ada");
take_name(name);
// `name` cannot be used here: it was moved into take_name
}Common Gotchas
A semicolon discards the return value
`a + b;` inside `-> i32` returns `()` and fails to compile. Drop the semicolon, or write `return a + b;`.
No default arguments and no overloads
Write two functions, or take `Option`, or use a builder. The compiler will not pick a function by arity or type the way C++ does.
Ownership is the signature
`fn f(s: String)` is not the same contract as `fn f(s: &str)`. The first steals the string; the second only reads it.
Nested `fn` items cannot capture
A function inside another function is not a closure. Use `let c = |x| ...` when you need the outer bindings.