# Cairo Closures

> Explain Cairo closures, syntax, capture rules, type inference, and Fn traits; use when a request involves anonymous functions or passing behavior as a parameter in Cairo.

- Skill: `teddyjfpender/cairo-closures` (Agent Skill, multi-file: 2 files)
- Install (CLI): `npx skillmds@latest add teddyjfpender/cairo-closures`
- Raw SKILL.md: https://api.skillmd.com/api/skills/teddyjfpender/cairo-closures/raw
- Safety review: pending
- Works with: Claude Code, Claude.ai, OpenAI Codex
- Category: AI & ML
- Author: teddyjfpender (https://skillmd.com/u/teddyjfpender)
- Updated: 2026-09-17
- Page: https://skillmd.com/skills/teddyjfpender/cairo-closures

---


# Cairo Closures

## Overview
Guide closure syntax and how closures capture environment values in Cairo.

## Quick Use
- Read `references/closures.md` before answering.
- Use concise examples like `|x| x * 2` and multi-line closures with `{}`.
- Mention the current limitation on capturing mutable variables.

## Response Checklist
- Use pipe syntax for parameters and infer types when possible.
- Explain capture of outer bindings and when that affects trait bounds.
- Choose `FnOnce`, `FnMut`, or `Fn` based on how the closure uses captured values.

## Example Requests
- "How do I write a closure in Cairo?"
- "Why is the closure type inferred as `u8`?"
- "What is the difference between `FnOnce`, `FnMut`, and `Fn`?"

## Cairo by Example
- [Closures](https://cairo-by-example.xyz/fn/closures)
- [Capturing](https://cairo-by-example.xyz/fn/closures/capture)
- [As input parameters](https://cairo-by-example.xyz/fn/closures/input_parameters)
- [Input functions](https://cairo-by-example.xyz/fn/closures/input_functions)
- [Higher Order Functions](https://cairo-by-example.xyz/fn/hof)

