I'm trying to write a function that composes two functions. The initial design is pretty simple: a function that takes two functions and returns a composed function which I can then compose with other functions, since Rust doesn't have rest parameters. I've run into a wall built with frustrating non-helpful compiler errors.

My compose function:

fn compose<'a, A, B, C, G, F>(f: F, g: G) -> Box<Fn(A) -> C + 'a> where F: 'a + Fn(A) -> B + Sized, G: 'a + Fn(B) -> C + Sized, { Box::new(move |x| g(f(x))) }

How I would like to use it:

fn main() { let addAndMultiply = compose(|x| x * 2, |x| x + 2); let divideAndSubtract = compose(|x| x / 2, |x| x - 2); let finally = compose(*addAndMultiply, *divideAndSubtract); println!("Result is {}", finally(10)); }

The compiler doesn't like that, no matter what I try, the trait bounds are never satisfied. The error is: