Your First egui Window — Rust GUI Tutorial (Learn egui in Neovim Ep.1)
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 newand addeframeas a dependency for native GUI support. - Define a struct to hold your app state and implement
Defaultfor easy initialization. - Implement the
epi::Apptrait, especially theupdatemethod to build your UI using egui widgets. - Use
CentralPanelto define the main window area and add widgets likeheading()andlabel()for text display. - Run and build your app with
cargo buildandcargo runinside Neovim, navigating files efficiently with Neo-tree.