Why does a mutable borrow of a closure through DerefMut not work?

前端 未结 1 501
日久生厌
日久生厌 2020-11-27 23:15

I am trying to mutably borrow a mutable variable. Deref and DerefMut are implemented for Foo, but compilation fails:

u         


        
相关标签:
1条回答
  • 2020-11-28 00:18

    This is a known issue regarding how the function traits are inferred through Deref. As a workaround, you need to explicitly get a mutable reference by doing a mutable reborrow:

    let mut t = Foo;
    (&mut *t)();
    

    or by calling DerefMut::deref_mut:

    let mut t = Foo;
    t.deref_mut()();
    
    0 讨论(0)
提交回复
热议问题