[gtk-web] Update the Rust examples



commit 0c559094782a5f6df7e98056fc4c892f5d026dc4
Author: Emmanuele Bassi <ebassi gnome org>
Date:   Wed Feb 19 18:10:18 2020 +0000

    Update the Rust examples
    
    And add a Rust snippet to the front page.

 _data/sample_codes.yml    | 29 +++++++++++++++++++++++++++++
 collections/_docs/rust.md | 33 +++++++++++++++++++++------------
 2 files changed, 50 insertions(+), 12 deletions(-)
---
diff --git a/_data/sample_codes.yml b/_data/sample_codes.yml
index 85a74fb..8e57258 100644
--- a/_data/sample_codes.yml
+++ b/_data/sample_codes.yml
@@ -47,6 +47,35 @@ codes:
       # Run the application
       app.run(None)
       ```
+  - name: Rust
+    ext: rs
+    snippet: |
+      ```rust
+      use gio::prelude::*;
+      use gtk::prelude::*;
+      use glib::clone;
+
+      use std::env::args;
+
+      fn on_activate(application: &gtk::Application) {
+          let window = gtk::ApplicationWindow::new(application);
+          let button = gtk::Button::new_with_label("Hello World!");
+
+          button.connect_clicked(clone!(@weak window => move |_| window.destroy()));
+
+          window.add(&button);
+          window.show_all();
+      }
+
+      fn main() {
+          let application =
+              gtk::Application::new(Some("com.github.gtk-rs.examples.basic"), Default::default())
+                  .expect("Initialization failed...");
+
+          application.connect_activate(|app| on_activate(app));
+          application.run(&args().collect::<Vec<_>>());
+      }
+      ```
   - name: C
     ext: c
     snippet: |
diff --git a/collections/_docs/rust.md b/collections/_docs/rust.md
index 301f660..cb6bf51 100644
--- a/collections/_docs/rust.md
+++ b/collections/_docs/rust.md
@@ -16,20 +16,29 @@ There are also a growing number of examples and thorough tests of language featu
 ## A Hello World app
 
 ```rust
-extern crate gtk;
+use gio::prelude::*;
 use gtk::prelude::*;
-use gtk::{ButtonsType, DialogFlags, MessageType, MessageDialog, Window};
+use glib::clone;
+
+use std::env::args;
+
+fn on_activate(application: &gtk::Application) {
+    let window = gtk::ApplicationWindow::new(application);
+    let button = gtk::Button::new_with_label("Hello World!");
+
+    button.connect_clicked(clone!(@weak window => move |_| window.destroy()));
+
+    window.add(&button);
+    window.show_all();
+}
 
 fn main() {
-    if gtk::init().is_err() {
-        println!("Failed to initialize GTK.");
-        return;
-    }
-    MessageDialog::new(None::<&Window>,
-                      DialogFlags::empty(),
-                      MessageType::Info,
-                      ButtonsType::Ok,
-                      "Hello World").run();
+    let application =
+        gtk::Application::new(Some("com.github.gtk-rs.examples.basic"), Default::default())
+            .expect("Initialization failed...");
+
+    application.connect_activate(|app| on_activate(app));
+    application.run(&args().collect::<Vec<_>>());
 }
 ```
 
@@ -51,4 +60,4 @@ If you want to get in touch with the original source files, you can visit the pr
 
 * Project: [https://github.com/gtk-rs/gtk](https://github.com/gtk-rs/gtk)
 * Docs: [https://gtk-rs.org/docs/gtk/](https://gtk-rs.org/docs/gtk/)
-* Tutorial: [https://gtk-rs.org/docs-src/tutorial/](https://gtk-rs.org/docs-src/tutorial/)
\ No newline at end of file
+* Tutorial: [https://gtk-rs.org/docs-src/tutorial/](https://gtk-rs.org/docs-src/tutorial/)


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]