Back to Blog

Your First egui Window — Rust GUI Tutorial (Learn egui in Neovim Ep.1)

Sandy LaneSandy Lane

Video: Your First egui Window — Rust GUI Tutorial (Learn egui in Neovim Ep.1) by Taught by Celeste AI - AI Coding Coach

Watch full page →

Your First egui Window — Rust GUI Tutorial

Learn how to create a simple desktop GUI application in Rust using the egui library and eframe for native windowing. This example demonstrates setting up a new Rust project, adding dependencies, and writing a basic egui app with a window that displays a heading and labels, all within Neovim.

Code

use eframe::{egui, epi};

// Define the application state struct
struct MyApp;

// Provide a default implementation for MyApp
impl Default for MyApp {
  fn default() -> Self {
    Self {}
  }
}

// Implement the eframe App trait for MyApp
impl epi::App for MyApp {
  fn name(&self) -> &str {
    "My First egui Window"
  }

  // Called each frame to update the UI
  fn update(&mut self, ctx: &egui::Context, _frame: &epi::Frame) {
    egui::CentralPanel::default().show(ctx, |ui| {
      ui.heading("Welcome to egui!");   // Add a heading
      ui.label("This is your first egui window.");  // Add a label
      ui.label("Built with Rust and eframe.");        // Another label
    });
  }
}

fn main() {
  // Launch the native eframe application
  let options = eframe::NativeOptions::default();
  eframe::run_native(Box::new(MyApp::default()), options);
}

Key Points

  • Create a new Rust project with cargo new and add eframe as a dependency for native GUI support.
  • Define a struct to hold your app state and implement Default for easy initialization.
  • Implement the epi::App trait, especially the update method to build your UI using egui widgets.
  • Use CentralPanel to define the main window area and add widgets like heading() and label() for text display.
  • Run and build your app with cargo build and cargo run inside Neovim, navigating files efficiently with Neo-tree.