When tail calling a function, instead of its stack frame being added to the
stack, the stack frame of the caller is directly replaced with the callee's.
This means that as long as a loop in a call graph only uses tail calls, the
stack growth will be bounded.
This is useful for writing functional-style code (since it prevent recursion
from exhausting resources) or for code optimization (since a tail call
*might* be cheaper than a normal call, tail calls can be used in a similar
manner to computed goto).
pub fn fibonacci(n: u64, a: u64, b: u64) -> u64 {
match n {
0 => a,
1 => b,
_ => become fibonacci(n - 1, b, a + b),
}
}
a2128•1h ago
From documentation, and a test in the PR: