锈蚀关闭错误 - > ...由当前函数|拥有捕获移动值:

时间:2017-05-10 17:42:38

标签: rust gtk-rs

此代码:

<bean
    id      = "httpStatusReturningLogoutSuccessHandler"
    class   = "org.springframework.security.web.authentication.logout.HttpStatusReturningLogoutSuccessHandler"
/>

产生错误:

//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 或类似的?

1 个答案:

答案 0 :(得分: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稍后会在类似的地方使用