【发布时间】:2018-02-12 19:59:17
【问题描述】:
我正在尝试增加 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
【问题讨论】:
-
ButtonExt::connect_clicked确实需要具有'static生命周期的函数。相关(围绕线程,但错误相同):stackoverflow.com/a/28661524/1233251