Skip to Content
Language in depth

Functions and closures

Parameters are lent for the call, mut is marked at both ends, lambdas may recurse, captures act as call-time arguments, and there is one function per name.

A fn lends its parameters for the call, and a parameter it mutates is marked mut where it is declared and where it is called. A lambda is an expression; bound with let, it may call itself. There is no overloading: one name is one function. The normative rules are chapter 6 of the spec; ownership of what a function borrows is ownership and mutation.

Parameters are lent; mut shows at the call

A body is one = expr or a block whose last expression is its value; return is for early exits. A parameter the function may change is mut in the signature and again at the call, so a reader of the call site sees what can change:

Output
Press Run (Ctrl+Enter).

Arguments may be named, parameters may have defaults, and parameters after ; are named-only. Options are always named arguments (by:), never _by twins:

Output
Press Run (Ctrl+Enter).

(§6.1)

Lambdas and nested functions are one thing

Nested fns and lambdas follow the same rules and differ only in spelling. A let-bound lambda may call itself; when recursion leaves the return type open, annotate it:

Output
Press Run (Ctrl+Enter).

A lambda with k parameters, passed where a function of one k-tuple is expected, unpacks it, so (i, x) => over .items needs no ((i, x)) =>:

Output
Press Run (Ctrl+Enter).

Captures act as call-time arguments

A closure holds no borrow between calls. Each call lends what the closure reads and mutably lends what it writes, for that call only. So a closure may update a captured var, two closures may capture the same one, and the borrow rules are checked at each call site:

Output
Press Run (Ctrl+Enter).

A captured variable can't be moved while the closure is alive, and a closure can't outlive what it captures. Every call through a known function value is specialized: there is no std::function and no indirect call in the C++ (§6.2).

Decorators: a closed set

@memo(array) caches a function in a dense table over its parameters' bounded types, allocated on the first call. The storage is spelled, never picked: @memo(map) uses a hash map instead.

Output
Press Run (Ctrl+Enter).

The cache lives as long as the declaring block, so below @!multitest it is per test case. A memo function may not do I/O or write to what it captured. The other decorators are @get (an (O(1)) read without parentheses), the tool roles (@test, @alt, @gen, @check, @interactor) and the unsafe tier (@trusted, @cpp); user libraries declare their own as ordinary generic functions (§6.4). Generators (-> yields T) are in sequences and views.

Why it's designed this way

mut at both ends. Mutation surprises are a P0 bug class. A C++ vector<int>& parameter is invisible at the call, so f(a) may or may not change a; Python's shared references are the same with no marker at all. Marking the call costs four characters and makes every possible change visible where it happens.

No overloading. decision 127 chose one name, one signature, so a call never depends on which overload inference happens to pick, and an error names one function instead of listing candidates. It also allowed recursive lambdas. decision 60 made options named arguments instead of sort_by-style twins, and decision 324 settled variadic named arguments.

Lambda and nested fn are the same. In Rust a nested fn can't capture and a closure can, so turning one into the other changes what the body may touch. decision 144 removed the difference so a helper can move between the two spellings without changing what it may capture. decision 159 lets a lambda unpack a tuple argument without changing the callee's signature, which is why it isn't overloading.

Captures borrow per call. The rejected alternative, a closure holding its borrows for its whole life as in Rust, would forbid the everyday pattern of a DFS helper and the main loop both updating seen. Borrowing per call keeps that legal and still checks every conflicting use.

Specialized calls. A comparator or fold operation must cost what an inlined C++ lambda costs. The alternative, a type-erased function value like std::function, puts an indirect call (and sometimes an allocation) in the hot loop where nothing in the source shows it, so function values are never type-erased.

On this page