Hello
I try to convert this example to a struct/impl:
https://github.com/gtk-rs/examples/blob/master/src/bin/basic.rs
extern crate gio;
extern crate gtk;
use gio::prelude::*;
use gtk::prelude::*;
pub struct MyApp {
app: gtk::Application
}
impl MyApp {
pub fn new() -> MyApp {
let app = gtk::Application::new("com.github.gtk-rs.examples.basic",
Default::default())
.expect("Initialization failed...");
let my = MyApp{app: app};
my.app.connect_activate(|app| {
my.build_ui(app);
});
return my;
}
pub fn run(&self, args: &[String]) {
self.app.run(args);
}
fn build_ui(&self, application: >k::Application) {
let window = gtk::ApplicationWindow::new(application);
window.set_title("First GTK+ Program");
window.show_all();
}
}
use std::env::args;
fn main() {
println!("Hello, world!");
let app = MyApp::new();
app.run(&args().collect::<Vec<_>>())
}
and it complain on:
error[E0373]: closure may outlive the current function, but it borrows `my`, which is owned by the current function
--> src/main.rs:61:33
|
61 | my.app.connect_activate(|app| {
| ^^^^^ may outlive borrowed value `my`
62 | my.build_ui(app);
| -- `my` is borrowed here
note: function requires argument type to outlive `'static`
--> src/main.rs:61:9
|
61 | / my.app.connect_activate(|app| {
62 | | my.build_ui(app);
63 | | });
| |__________^
help: to force the closure to take ownership of `my` (and any other referenced variables), use the `move` keyword
|
61 | my.app.connect_activate(move |app| {
| ^^^^^^^^^^
error[E0505]: cannot move out of `my` because it is borrowed
--> src/main.rs:64:16
|
61 | my.app.connect_activate(|app| {
| - ----- borrow of `my` occurs here
| _________|
| |
62 | | my.build_ui(app);
| | -- borrow occurs due to use in closure
63 | | });
| |__________- argument requires that `my` is borrowed for `'static`
64 | return my;
| ^^ move out of `my` occurs here
error: aborting due to 2 previous errors
I also try to use the help suggest and change to:
my.app.connect_activate(move |app| {
my.build_ui(app);
});
But then I got:
error[E0505]: cannot move out of `my` because it is borrowed
--> src/main.rs:61:33
|
61 | my.app.connect_activate(move |app| {
| ------ ---------------- ^^^^^^^^^^ move out of `my` occurs here
| | |
| | borrow later used by call
| borrow of `my.app` occurs here
62 | my.build_ui(app);
| -- move occurs due to use in closure
error[E0382]: use of moved value: `my`
--> src/main.rs:64:16
|
61 | my.app.connect_activate(move |app| {
| ---------- value moved into closure here
62 | my.build_ui(app);
| -- variable moved due to use in closure
63 | });
64 | return my;
| ^^ value used here after move
|
= note: move occurs because `my` has type `MyApp`, which does not implement the `Copy` trait
error: aborting due to 2 previous errors
Thanks