Ошибки закрытия ржавчины -> ... которой владеет текущая функция | захват перемещенного значения:

Этот код:

//let seen_cell = std::cell::RefCell::new(window_0);
window_0.connect_delete_event(|_, _| {

    //window_0.destroy();
    window.hide();
    Inhibit(true)  
    });

button_0.connect_clicked(|_|{  
    window.show_all(); 
    }  
);

Выдает ошибки:

error[E0373]: closure may outlive the current function, but it borrows `window`, which is owned by the current function
   --> src/main.rs:192:36
    |
192 |     window_0.connect_delete_event( |_, _| {
    |                                    ^^^^^^ may outlive borrowed value `window`
...
195 |         window.hide();
    |         ------ `window` is borrowed here
    |
help: to force the closure to take ownership of `window` (and any other referenced variables), use the `move` keyword, as shown:
    |     window_0.connect_delete_event( move |_, _| {

error[E0373]: closure may outlive the current function, but it borrows `window`, which is owned by the current function
   --> src/main.rs:199:30
    |
199 |     button_0.connect_clicked(|_|{
    |                              ^^^ may outlive borrowed value `window`
200 |         window.show_all();
    |         ------ `window` is borrowed here
    |
help: to force the closure to take ownership of `window` (and any other referenced variables), use the `move` keyword, as shown:
    |     button_0.connect_clicked(move |_|{

Если я попробую это:

//let seen_cell = std::cell::RefCell::new(window_0);
window_0.connect_delete_event(move |_, _| {

    //window_0.destroy();
    window.hide();
    Inhibit(true)  
    });

button_0.connect_clicked(|_|{  
    window.show_all(); 
    }  
);

Я получаю ошибки:

error[E0373]: closure may outlive the current function, but it borrows `window`, which is owned by the current function
   --> src/main.rs:199:30
    |
199 |     button_0.connect_clicked(|_|{
    |                              ^^^ may outlive borrowed value `window`
200 |         window.show_all();
    |         ------ `window` is borrowed here
    |
help: to force the closure to take ownership of `window` (and any other referenced variables), use the `move` keyword, as shown:
    |     button_0.connect_clicked(move |_|{

error[E0382]: capture of moved value: `window`
   --> src/main.rs:199:30
    |
192 |     window_0.connect_delete_event(move |_, _| {
    |                                   ----------- value moved (into closure) here
...
199 |     button_0.connect_clicked(|_|{
    |                              ^^^ value captured here after move
    |
    = note: move occurs because `window` has type `gtk::Window`, which does not implement the `Copy` trait

Если я попробую это:

//let seen_cell = std::cell::RefCell::new(window_0);
window_0.connect_delete_event(move |_, _| {

    //window_0.destroy();
    window.hide();
    Inhibit(true)  
    });

button_0.connect_clicked(move|_|{  
    window.show_all(); 
    }  
);

Я получаю ошибки:

error[E0382]: capture of moved value: `window`
   --> src/main.rs:200:9
    |
192 |     window_0.connect_delete_event(move |_, _| {
    |                                   ----------- value moved (into closure) here
...
200 |         window.show_all();
    |         ^^^^^^ value captured here after move
    |
    = note: move occurs because `window` has type `gtk::Window`, which does not implement the `Copy` trait

Я читал похожие вопросы, но мне не удалось решить это дело. Как я могу решить это наилучшим образом, возможно, с помощью Arc или похожие?

1 ответ

Я решил вышеизложенное с помощью макроса, который я взял из некоторых примеров проектов gtk-rs

macro_rules! clone {
        (@param _) => ( _ );
        (@param $x:ident) => ( $x );
        ($($n:ident),+ => move || $body:expr) => (
            {
                $( let $n = $n.clone(); )+
                move || $body
            }
        );
        ($($n:ident),+ => move |$($p:tt),+| $body:expr) => (
            {
                $( let $n = $n.clone(); )+
                move |$(clone!(@param $p),)+| $body
            }
        );
    }

Взяв описанный случай, я использовал его следующим образом:

window_0.connect_delete_event(clone!(window => move |_, _| {

//window_0.destroy();
window.hide();
Inhibit(true)  
}));

button_0.connect_clicked(clone!(window => move |_|{  
    window.show_all(); 

}));

Это соответствующая часть window_0.connect_delete_event(clone!(window => move, В моем случае это также относится к button_0.connect_clicked так как window Используется позже в аналогичном месте

Другие вопросы по тегам