rust-tailcall
TailCall Implementation in Rust
This document explains how to implement and use tail call optimization (TCO) using Rust's type system and functional style. The crate is a rust implementation of The Art of Simplicity_Venkat Subramaniam.
📖 What is Tail Call?
A tail call is a function call performed as the final action of a function. In languages without proper tail call optimization, deep recursion can cause a stack overflow.
Rust does not guarantee tail call optimization at the compiler level, but we can simulate it using enums and iteration.
✅ TailCall in Rust
We define a TailCall enum that represents two states:
Continue: A pending computation wrapped in a closure.Done: A completed result.
✅ TailCall in basic example
use TailCall;
// Example usage: factorial function using TailCall
✅ TailCall in bigint example
use BigUint;
use ;
use TailCall;
/// Computes the factorial of `n` using tail recursion.
///
/// # Arguments
/// * `n` - The number to compute the factorial for.
/// * `acc` - The accumulator holding the running result.
///
/// # Returns
/// A `TailCall` that will eventually produce the factorial of `n`.
🔗 Helpful Links & Resources
📜 License
- Mozilla Public License 2.0