类型必须满足静电生存期 [英] Type must satisfy the static lifetime

查看:43
本文介绍了类型必须满足静电生存期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试增加Rust和GTK-RS应用程序的结构,但是我不知道如何处理事件连接。我发现问题在错误的生存期内,但我真的不明白如何解决它。

#[derive(Debug)]
struct CreatingProfileUI {
    window: gtk::MessageDialog,
    profile_name_entry: gtk::Entry,
    add_btn: gtk::Button,
    cancel_btn: gtk::Button,
}

#[derive(Debug)]
struct UI {
    window: gtk::Window,

    // Header
    url_entry: gtk::Entry,
    open_btn: gtk::Button,

    // Body
    add_profile_btn: gtk::Button,
    remove_profile_btn: gtk::Button,
    profiles_textview: gtk::TextView,

    // Creating profile
    creating_profile: CreatingProfileUI,

    // Statusbar
    statusbar: gtk::Statusbar,
}

impl UI {
    fn init(&self) {
        self.add_profile_btn
            .connect_clicked(move |_| { &self.creating_profile.window.run(); });
    }
}

我收到以下错误:

error[E0477]: the type `[closure@src/main.rs:109:46: 111:6 self:&UI]` does not fulfill the required lifetime
   --> src/main.rs:109:30
    |
109 |         self.add_profile_btn.connect_clicked(move |_| {
    |                              ^^^^^^^^^^^^^^^
    |
    = note: type must satisfy the static lifetime

推荐答案

无法将非静电引用移入gtk回调。您需要分配静电或堆(例如,在Box/RefCell/Rc/等中)。

回调不是从连接到信号的作用域调用的,而是在稍后从主循环调用的。要求您传递到闭包中的任何内容都仍然是活动的,也就是'static,堆分配或在main和主循环运行位置之间的堆栈上分配的内容。最后一部分目前不能用Rust/gtk-rs很好地表达出来。

请参阅the example at the bottom in the gtk-rs docs for an example。它使用Rc<RefCell<_>>

这篇关于类型必须满足静电生存期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆