Effect Handlers
Tutorial Video
Text description (accessibility)
This video demonstrates the "Effect Handlers" functional Rust example. Difficulty level: Expert. Key concepts covered: Functional Programming. An effect handler is the runtime component that intercepts effects, provides a value, and resumes the computation. Key difference from OCaml: 1. **Handler composition**: OCaml's nested `match_with` calls compose handlers automatically; Rust's simulation requires explicit forwarding of unhandled effects.
Tutorial
The Problem
An effect handler is the runtime component that intercepts effects, provides a value, and resumes the computation. Different handlers for the same effects give different behaviors — the same program can run in a logging context, a testing context, or a production context. This is the effect-system equivalent of the free monad interpreter pattern, but operating at the runtime level rather than the data-structure level.
🎯 Learning Outcomes
Code Example
#![allow(clippy::all)]
// Stub — awaiting conversion from OCaml source.Key Differences
match_with calls compose handlers automatically; Rust's simulation requires explicit forwarding of unhandled effects.eio) is built on effects; Rust's async/await uses a different mechanism (state machine transformation).OCaml Approach
OCaml 5's match_with is the native handler:
let with_state initial program =
let state = ref initial in
match_with program ()
{ effc = fun (type a) (eff : a eff) ->
match eff with
| Get -> Some (fun k -> continue k !state)
| Put v -> Some (fun k -> state := v; continue k ())
| _ -> None }
Nested match_with handlers compose naturally — each handles its own effects and forwards others upward.
Full Source
#![allow(clippy::all)]
// Stub — awaiting conversion from OCaml source.Deep Comparison
OCaml vs Rust: Effect Handler
Overview
See the example.rs and example.ml files for detailed implementations.
Key Differences
| Aspect | OCaml | Rust |
|---|---|---|
| Type system | Hindley-Milner | Ownership + traits |
| Memory | GC | Zero-cost abstractions |
| Mutability | Explicit ref | mut keyword |
| Error handling | Option/Result | Result<T, E> |
See README.md for detailed comparison.
Exercises
with_transaction handler that wraps all state operations in a simulated transaction, rolling back on error.