[英]Type must satisfy the static lifetime
我正在嘗試增加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
,堆分配或分配。 目前使用Rust / GTK-rs無法很好地表達最后一部分。
有關示例,請參閱gtk-rs文檔底部的示例 。 它使用Rc<RefCell<_>>
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.