[英]Type must satisfy the static lifetime
I'm trying to increase the structure of a Rust and GTK-RS application, but I cannot figure out how to deal with event connections. 我正在尝试增加Rust和GTK-RS应用程序的结构,但我无法弄清楚如何处理事件连接。 I see that the problem is in wrong lifetime, but I do not really understand how it could be fixed.
我发现问题是在错误的生命周期中,但我真的不明白它是如何修复的。
#[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(); });
}
}
And I get this error: 我收到这个错误:
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
You can't move non-static references into GTK callbacks. 您无法将非静态引用移动到GTK回调中。 You need something static or something heap allocated (eg in a
Box
/ RefCell
/ Rc
/etc.). 您需要静态或分配堆的东西(例如在
Box
/ RefCell
/ Rc
/等中)。
Callbacks are not called from the scope where you connect to the signal, but at some later point from the main loop. 不会从连接到信号的作用域调用回调,而是在主循环的某个稍后点调用回调。 It is required that whatever you pass into the closure is still alive then, which would be anything
'static
, heap-allocated or allocated on the stack between main and where the main loop runs. 无论你传递到闭包中的是什么,都要求它仍然存活,这将是主要循环和主循环之间的堆栈上的
'static
,堆分配或分配。 The last part can't currently be nicely expressed with Rust/GTK-rs. 目前使用Rust / GTK-rs无法很好地表达最后一部分。
See the example at the bottom in the gtk-rs docs for an example . 有关示例,请参阅gtk-rs文档底部的示例 。 It uses an
Rc<RefCell<_>>
. 它使用
Rc<RefCell<_>>
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.