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.
More Related Contents:
- What is the difference between iter and into_iter?
- How can I swap in a new value for a field in a mutable reference to a structure?
- How does for syntax differ from a regular lifetime bound?
- Why is this match pattern unreachable when using non-literal patterns?
- How to convert a String into a &’static str
- Proper way to return a new string in Rust
- Double mutable borrow error in a loop happens even with NLL on
- How to return a reference to a sub-value of a value that is under a mutex?
- Is it possible to make a type only movable and not copyable?
- How can this instance seemingly outlive its own parameter lifetime?
- How to use struct self in member method closure
- How do I call a function through a member variable?
- How do you pass a Rust function as a parameter?
- Why does removing return give me an error: expected type `()` but found type
- How do I implement a trait with a generic method?
- Linking the lifetimes of self and a reference in method
- Is there a way to create a function pointer to a method in Rust?
- How do I use conditional compilation with `cfg` and Cargo?
- Return an async function from a function in Rust
- How to implement `serde::Serialize` for a boxed trait object?
- Borrow two mutable values from the same HashMap
- Can I take a byte array and deserialize it into a struct?
- Is there any difference between matching on a reference to a pattern or a dereferenced value?
- Why does a lazy-static value claim to not implement a trait that it clearly implements?
- How do I convert a usize to a u32 using TryFrom?
- Can Cargo download and build dependencies without also building the application?
- How do I pass Rc to a function accepting Rc?
- Is there a way for me to use #[derive] on a struct or enum from a library without editing the actual library’s source code?
- Why is this Rust slower than my similar Python? [duplicate]
- How do I create a HashMap with type erased keys?