Implement Scala-like Trampoline to gain tail call optimization in Rust
Run Rustfmt. It will automatically fix such things as


Opening curly braces go on the same line as the function signature (unless the function signature is already multiple lines).
There is no comma at the end of a match arm that uses curly braces. 

Your function name has a typo: recurse_trampolin
Instead of taking a trait object reference (&amp;Fn(...) -&gt; ...), take a generic. This allows for better optimizations and avoids unnecessary indirection.




enum Trampoline&lt;A, R&gt; {
    Continue(A, R),
    End(R),
}

impl&lt;A, R&gt; Trampoline&lt;A, R&gt; {
    fn start&lt;F&gt;(function: F, mut arg: A, mut sum: R) -&gt; R
    where
        F: Fn(A, R) -&gt; Trampoline&lt;A, R&gt;,
    {
        loop {
            match function(arg, sum) {
                Trampoline::Continue(r_arg, r_sum) =&gt; {
                    arg = r_arg;
                    sum = r_sum;
                }
                Trampoline::End(result) =&gt; return result,
            }
        }
    }
}

fn recurse_trampoline(arg: i32, sum: i32) -&gt; Trampoline&lt;i32, i32&gt; {
    if arg == 0 {
        Trampoline::End(sum)
    } else {
        Trampoline::Continue(arg - 1, sum + arg)
    }
}

fn recurse_normal(arg: i32, sum: i32) -&gt; i32 {
    if arg == 0 {
        sum
    } else {
        recurse_normal(arg - 1, sum + arg)
    }
}

fn main() {
    println!("{}", recurse_normal(5, 0));
    println!("{}", Trampoline::start(recurse_trampoline, 5, 0));
}