How do I write a function that can compose `FnMut` closures?

爷,独闯天下 提交于 2019-12-25 03:14:15

问题


Here's a compose function that can compose Fn closures:

fn compose<'a, T1, T2, T3, F1, F2>(f: F1, g: F2) -> Box<Fn(T1) -> T3 + 'a>
    where F1: Fn(T1) -> T2 + 'a,
          F2: Fn(T2) -> T3 + 'a
{
    box move |x| g(f(x))
}

How do I make it so that this compose function can take FnMut closures? I tried:

fn compose<'a, T1, T2, T3, F1, F2>(f: F1, g: F2) -> Box<FnMut(T1) -> T3 + 'a>
    where F1: FnMut(T1) -> T2 + 'a,
          F2: FnMut(T2) -> T3 + 'a
{
    box move |x| g(f(x))
}

But it complains about:

error: cannot borrow captured outer variable in an `FnMut` closure as mutable
         box move |x| g(f(x))
                      ^
error: cannot borrow captured outer variable in an `FnMut` closure as mutable
         box move |x| g(f(x))
                        ^

Extending this, can it be made to work with FnOnce closures?


回答1:


Local variables f and g must be mutable:

fn compose<'a, T1, T2, T3, F1, F2>(mut f: F1, mut g: F2) -> Box<FnMut(T1) -> T3 + 'a>
    where F1: FnMut(T1) -> T2 + 'a,
          F2: FnMut(T2) -> T3 + 'a
{
    Box::new(move |x| g(f(x)))
}


来源:https://stackoverflow.com/questions/36284637/how-do-i-write-a-function-that-can-compose-fnmut-closures

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!