As explained in Shepmaster's answer, you can only move a value out of a variable once, and the compiler will prevent you from doing it a second time. I'll try to add a bit of specific context for this use case. Most of this is from my memory of having used GTK from C ages ago, and a few bits I just looked up in the gtk-rs documentation, so I'm sure I got some details wrong, but I think the general gist is accurate.
Let's first take a look at why you need to move the value into the closures in the first place. The methods you call on list_box
inside both closures take self
by reference, so you don't actually consume the list box in the closures. This means it would be perfectly valid to define the two closures without the move
specifiers – you only need read-only references to list_box
, you are allowed to have more than one read-only reference at once, and list_box
lives at least as long as the closures.
However, while you are allowed to define the two closures without moving list_box
into them, you can't pass the closures defined this way to gtk-rs: all functions connecting event handlers only accept "static" functions, e.g.
fn connect_search_changed<F: Fn(&Self) + 'static>(
&self,
f: F
) -> SignalHandlerId
The type F
of the handler has the trait bound Fn(&Self) + 'static
, which means that the closure either can't hold any references at all, or all references it holds must have static lifetime. If we don't move list_box
into the closure, the closure will hold a non-static reference to it. So we need to get rid of the reference before being able to use the function as an event handler.
Why does gtk-rs impose this limitation? The reason is that gtk-rs is a wrapper around a set of C libraries, and a pointer to the callback is eventually passed on to the underlying glib
library. Since C does not have any concept of lifetimes, the only way to do this safely is to require that there aren't any references that may become invalid.
We have now established that our closures can't hold any references. We still need to access list_box
from the closures, so what are our options? If you only have a single closure, using move
does the trick – by moving list_box
into the closure, the closure becomes its owner. However, we have seen that this doesn't work for more than one closure, because we can only move list_box
once. We need to find a way to have multiple owners for it, and the Rust standard library provides such a way: the reference-counting pointers Rc
and Arc
. The former is used for values that are only accessed from the current thread, while the latter is safe to move to other threads.
If I remember correctly, glib executes all event handlers in the main thread, and the trait bounds for the closure reflect this: the closure isn't required to be Send
or Sync
, so we should be able to make do with Rc
. Morevoer, we only need read access to list_box
in the closures, so we don't need RefCell
or Mutex
for interior mutability in this case. In summary, all you need is probably this:
use std::rc::Rc;
let list_box: gtk::ListBox = builder.get_object("list_box").unwrap();
let list_box_1 = Rc::new(list_box);
let list_box_2 = list_box_1.clone();
Now you have two "owned" pointers to the same list box, and these pointers can be moved into the two closures.
Disclaimer: I couldn't really test any of this, since your example code isn't self-contained.