Skip to content

Overview

5 min read

Functions are the workhorse of any program, and you already write them every day in TypeScript and JavaScript. This section maps each piece you know — declarations, parameters, return values, arrow functions, higher-order functions, function pointers, and recursion — onto its idiomatic Rust counterpart. The syntax is close enough to feel familiar, but a few deeper ideas (typed signatures as hard contracts, the expression-oriented body, the Fn/FnMut/FnOnce closure traits, and the absence of default/rest parameters and guaranteed tail-call optimization) reshape how you read and write Rust code.



By the end of this section, you will be able to:

  • Write Rust functions with fn, snake_case names, mandatory typed parameters, and a -> Type return arrow
  • Use the tail expression (last line, no semicolon) as the return value, and reserve return for early exits
  • Reproduce default, optional, and rest parameters with Option<T>, slices &[T], Default structs, and the builder pattern
  • Return multiple values as a tuple (or a named struct) instead of mutating out-parameters
  • Translate arrow functions into Rust closures and understand capture by &, &mut, and move
  • Distinguish the three closure traits — Fn, FnMut, FnOnce — and accept the weakest one that works
  • Take closures as parameters (impl Fn) and return them (impl Fn or Box<dyn Fn>)
  • Pass named functions and constructors as function pointers (fn(T) -> R)
  • Write recursion safely, knowing Rust has no guaranteed tail-call optimization, and reach for iteration or Box when appropriate

#TopicWhat it covers
1Basic Functions and Signaturesfn definitions, typed parameters, the -> return type, statements vs expressions; vs TS declarations
2Function ParametersNo default/rest params; Option<T>, slices &[T], Default structs, builders, “overloading” via traits
3Return ValuesReturn types, implicit tail-expression return, the unit type (), early return, returning tuples
4Arrow Functions vs Closures(args) => becomes |args|; Fn/FnMut/FnOnce; capture by reference vs move; the move keyword
5Higher-Order Functionsmap/filter/reduce (lazy iterators); taking impl Fn and returning impl Fn / Box<dyn Fn>
6Function PointersThe fn type, passing named functions, function items vs fn pointers vs closures
7RecursionRecursive functions, no guaranteed TCO, stack depth, iterative alternatives, recursive enums with Box

After completing this section, a TypeScript/JavaScript developer should be able to:

  1. Read any Rust function signature and explain its parameters and return type as a compiler-enforced contract.
  2. Explain why a stray semicolon changes a function’s return value to (), and fix the resulting mismatched types error.
  3. Choose the right idiom for “flexible” arguments: Option<T>, a slice, a Default struct, a builder, or a trait bound.
  4. Write closures with inferred types, decide when move is required, and pick the correct Fn/FnMut/FnOnce bound.
  5. Build lazy iterator pipelines (iter().filter().map().collect()) as the replacement for eager Array.prototype chains.
  6. Decide between a fn pointer and a generic impl Fn bound for a callback parameter.
  7. Recognize when recursion is safe (shallow, recursive data) versus when iteration is the safer Rust default.

This section assumes you have completed:

  • Section 02: Basics — variables and mutability (let mut is essential for FnMut), the basic types you will pass as parameters, and especially the statement-vs-expression distinction that underpins tail-expression returns.

If the expression-oriented model (let x = { ...; a + b };) feels unfamiliar, re-read the Basics section before starting here.

Note: Several topics in this section preview concepts covered fully later: ownership and move (Section 05), Option/Result and the ? operator (Section 08), generics and traits (Section 09), and Box/Rc for recursive types (Section 10). You do not need those sections first — the links are there for when you want to go deeper.


  • Reading: 3-4 hours
  • Hands-on Practice & Exercises: 3-4 hours
  • Total: 6-8 hours

A reasonable order is the list order above: basics → parameters → return values → closures → higher-order → function pointers → recursion. Closures (topic 4) are the conceptual heart of the section, so do not skip them.