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.
Agent 可直接安装
这个资产可安装;Agent 先选择当前运行时、检查安装计划,再运行匹配命令。
npx -y tokrepo@latest install a85cf74f-3862-11f1-9bc6-00163e2b0d79 --target codex先 dry-run 确认安装计划,再运行此命令。
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.
常见问题
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.
引用来源 (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
讨论
相关资产
Dear ImGui — Bloat-Free Immediate Mode GUI for C++ That Powers Every Game Tool
Dear ImGui is the immediate-mode GUI library used by virtually every game engine for in-game editors, debug overlays, and tooling. Minimal dependencies, minimal state, maximum productivity.
Nuklear — Minimal ANSI C Immediate-Mode GUI Library
A single-header, portable, zero-dependency immediate-mode GUI toolkit written in ANSI C for embedding lightweight interfaces in games, tools, and embedded applications.
Ratatui — Terminal User Interface Library for Rust
Ratatui is a Rust library for building rich terminal user interfaces (TUIs). It provides widgets for tables, charts, lists, paragraphs, tabs, and more — enabling you to build beautiful, interactive terminal applications with immediate-mode rendering.
Dear PyGui — GPU-Accelerated Python GUI Framework
Build fast, interactive desktop applications and data tools in Python using an immediate-mode GPU-rendered graphical interface with zero external dependencies.