#[tailcall]
Expand description
Transforms a function definition so that all recursive calls within the body are guaranteed to use a single stack frame (via tail recursion).
§Example
use tailcall_impl::tailcall;
fn factorial(input: u64) -> u64 {
#[tailcall]
fn factorial_inner(accumulator: u64, input: u64) -> u64 {
if input > 0 {
factorial_inner(accumulator * input, input - 1)
} else {
accumulator
}
}
factorial_inner(1, input)
}
§Requirements
- All recursive calls must be in tail form:
ⓘ
use tailcall_impl::tailcall;
#[tailcall]
fn factorial(input: u64) -> u64 {
if input > 0 {
input * factorial(input - 1)
// ^^^^^^^ This is not allowed.
} else {
1
}
}
- Methods (functions which bind
self
in the arguments list) are not supported:
ⓘ
trait Factorialable {
fn factorial(self) -> Self {
self.calc_factorial(1)
}
fn calc_factorial(self, accumulator: u64) -> u64;
}
impl Factorialable for u64 {
#[tailcall]
fn calc_factorial(self, accumulator: u64) -> u64 {
// ^^^^ This is not allowed.
if self > 0 {
(self - 1).calc_factorial(self * accumulator)
} else {
accumulator
}
}
}