gpt4 book ai didi

rust - 类型必须满足静态生命周期

转载 作者:行者123 更新时间:2023-11-29 07:50:10 25 4
gpt4 key购买 nike

我正在尝试增加 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/etc.)。

回调不是从您连接到信号的范围调用的,而是在稍后的某个时间点从主循环调用的。要求你传入闭包的任何东西都还活着,这可以是任何东西 'static ,堆分配或分配在 main 和主循环运行位置之间的堆栈上。最后一部分目前无法用 Rust/GTK-rs 很好地表达。

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

关于rust - 类型必须满足静态生命周期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46039441/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com