egui — Easy-to-Use Immediate Mode GUI in Pure Rust That Runs Anywhere
egui is a pure-Rust immediate-mode GUI library that compiles to native, web (WASM), and embedded targets. The Rust ecosystem's answer to Dear ImGui — with a more modern API and first-class WASM support.
What it is
egui is a pure-Rust immediate mode GUI library. Unlike retained mode frameworks where you build a widget tree and the framework manages updates, egui redraws the entire UI every frame. You call functions like ui.button('Click me') and handle the result inline. No widget state management, no callback registration.
egui compiles to native desktop (via eframe), web browsers (via WASM), and embedded targets. It is the Rust ecosystem's answer to Dear ImGui, with a more idiomatic Rust API and first-class WebAssembly support.
How it saves time or tokens
egui's immediate mode API eliminates the complexity of widget lifecycle management. In retained mode frameworks, you create widgets, register callbacks, manage state, and handle updates. In egui, the UI logic is just Rust code that runs every frame. This makes prototyping tools and debug UIs fast.
Cross-platform compilation means one codebase for desktop and web. Write your UI once, compile to native with eframe or to WASM for browser deployment.
How to use
- Add egui to your Cargo.toml:
[dependencies]
eframe = "0.28"
- Write your UI:
use eframe::egui;
fn main() -> eframe::Result {
eframe::run_simple_native(
"My App",
eframe::NativeOptions::default(),
move |ctx, _frame| {
egui::CentralPanel::default().show(ctx, |ui| {
ui.heading("Hello egui");
if ui.button("Click me").clicked() {
println!("Button clicked");
}
});
},
)
}
- Run with
cargo runfor native, or compile to WASM withtrunk servefor web.
Example
A counter app with slider and label:
use eframe::egui;
struct MyApp {
counter: i32,
speed: f32,
}
impl eframe::App for MyApp {
fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
egui::CentralPanel::default().show(ctx, |ui| {
ui.heading(format!("Counter: {}", self.counter));
if ui.button("+1").clicked() {
self.counter += 1;
}
ui.add(egui::Slider::new(&mut self.speed, 0.0..=10.0).text("Speed"));
});
}
}
No widget IDs, no event handlers, no state synchronization. The UI is a function of your data.
Related on TokRepo
- AI tools for coding -- Development tools and frameworks for building applications
- Featured workflows -- Discover popular developer tools and configurations
Common pitfalls
- Expecting retained mode patterns. egui redraws every frame. Do not cache widget references or register callbacks. Instead, put your UI logic in the
updatefunction and let egui handle rendering. - Performance on complex UIs. Immediate mode redraws everything each frame. For UIs with thousands of widgets, use egui's built-in
ScrollAreawith lazy rendering or profile withpuffin. - WASM bundle size. A minimal egui WASM app is around 2MB. Use
wasm-optandtrunkoptimizations to reduce this for production web deployments.
Frequently Asked Questions
In immediate mode, you call UI functions every frame and handle results inline. There is no persistent widget tree. In retained mode (Qt, GTK), you create widgets once, register callbacks, and the framework manages updates. Immediate mode is simpler for tools and prototypes but redraws everything each frame.
Yes, for tool-like applications (dashboards, editors, configuration UIs). egui compiles to WebAssembly and runs in the browser via a canvas element. It is not designed for content-heavy websites where HTML/CSS would be more appropriate.
Both are immediate mode GUI libraries. Dear ImGui is C++ with bindings for many languages. egui is pure Rust with an idiomatic Rust API. egui has better WASM support and does not require a C++ toolchain. Dear ImGui has a larger ecosystem of widgets and extensions.
Yes. egui provides a Style struct that controls colors, spacing, rounding, and fonts. You can create light/dark themes or fully custom visual styles. The catppuccin-egui crate provides popular color schemes.
egui runs on Windows, macOS, Linux (native via eframe), web browsers (via WASM), and embedded targets. The eframe wrapper handles platform-specific windowing and input. For mobile, community support exists but is less mature than desktop.
Citations (3)
- egui GitHub— egui is a pure-Rust immediate mode GUI library
- eframe Documentation— eframe provides native and WASM integration for egui
- Dear ImGui FAQ— Immediate mode GUI design pattern
Related on TokRepo
Discussion
Related Assets
NAPI-RS — Build Node.js Native Addons in Rust
Write high-performance Node.js native modules in Rust with automatic TypeScript type generation and cross-platform prebuilt binaries.
Mamba — Fast Cross-Platform Package Manager
A drop-in conda replacement written in C++ that resolves environments in seconds instead of minutes.
Plasmo — The Browser Extension Framework
Build, test, and publish browser extensions for Chrome, Firefox, and Edge using React or Vue with hot-reload and automatic manifest generation.