Cannot pass self as callback parameter due to double borrowing

后端 未结 1 1747
我在风中等你
我在风中等你 2020-12-04 02:01

I\'m trying to call closure that was saved inside a struct but I\'d also like to pass the struct as an argument to the closure. Here\'s the specific piece of code.



        
相关标签:
1条回答
  • 2020-12-04 02:42

    The simplest solution is to break the callback out from self, like so:

    let callback = self.click_callback.take();
    if let Some(ref mut c) = callback {
        c(self);
    }
    self.click_callback = callback;
    self
    

    This temporarily replaces click_callback with None, hence why you have to put it back when you're done.

    0 讨论(0)
提交回复
热议问题