Swiftorial Logo
Home
Swift Lessons
Matchups
CodeSnaps
Tutorials
Career
Resources

Creating a GUI Application in Rust

Introduction

Graphical User Interface (GUI) applications enhance user interaction by providing a visual interface. Rust, known for its performance and safety, can be used to create GUI applications using libraries like GTK and Qt. This tutorial will guide you through the process of setting up a simple GUI application using Rust and GTK.

Setting Up Your Environment

To get started, you need to have Rust installed on your machine. You can install Rust using rustup, which is the recommended way. Open your terminal and run:

curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh

Once Rust is installed, you need to add the GTK library. If you are using a package manager like apt (for Ubuntu), you can install it by running:

sudo apt install libgtk-3-dev

For other systems, please refer to the official GTK installation guides.

Creating a New Rust Project

Create a new Rust project using the command:

cargo new rust_gui_app

Navigate to the project directory:

cd rust_gui_app

Next, add the gtk crate to your project by editing the Cargo.toml file:

echo 'gtk = "0.9"' >> Cargo.toml

Building Your First GUI Application

Open src/main.rs and replace its contents with the following code:

use gtk::prelude::*;
use gtk::{Button, Label, Window, WindowType};

fn main() {
    // Initialize GTK application
    gtk::init().expect("Failed to initialize GTK.");

    // Create a new window
    let window = Window::new(WindowType::Toplevel);
    window.set_title("Hello GTK!");
    window.set_default_size(350, 70);

    // Create a new label
    let label = Label::new(Some("Hello, World!"));

    // Create a button
    let button = Button::with_label("Click me!");
    button.connect_clicked(move |_| {
        label.set_text("Button clicked!");
    });

    // Arrange widgets in a vertical box
    let vbox = gtk::Box::new(gtk::Orientation::Vertical, 5);
    vbox.pack_start(&label, true, true, 0);
    vbox.pack_start(&button, true, true, 0);

    window.add(&vbox);
    
    window.connect_delete_event(|_, _| {
        gtk::main_quit();
        Inhibit(false)
    });

    window.show_all();
    gtk::main();
} 
                

This code creates a simple window with a label and a button. When the button is clicked, the label updates its text.

Running the Application

To run your application, navigate to your project directory in the terminal and execute:

cargo run

If everything is set up correctly, a window should appear displaying "Hello, World!" and a button. Clicking the button will change the label text.

Conclusion

You have successfully created a simple GUI application in Rust using the GTK library. This tutorial covered the setup process, project creation, and basic GUI elements. You can expand on this by adding more features and widgets to enhance your application.

Further Reading

To learn more about GTK and Rust, consider checking out the following resources: