Scripts2026年4月15日·1 分钟阅读

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.

Introduction

egui, by Emil Ernerfeldt, is the immediate-mode GUI library the Rust ecosystem rallied around. Its killer feature is universal targets: the same app compiles to native (via eframe), web (via WebAssembly), mobile, or inside another game engine (bevy_egui, three-egui). The API is type-safe, simple, and zero-boilerplate.

With over 28,000 GitHub stars, egui powers Rerun's data visualization UI, Bevy editors, the Iced/egui hybrid in Lapce, and dozens of Rust desktop apps. It's the default pick for Rust developers who want a GUI without wrestling with Qt or GTK bindings.

What egui Does

egui builds a retained draw list each frame from immediate-mode calls. Your code constructs the UI with ui.button(), ui.label(), etc.; egui computes layout, hit-tests inputs, and emits graphics primitives. eframe wraps it for native + web; bevy_egui embeds it in a Bevy game; egui_web is the direct WASM target.

Architecture Overview

Your Rust code (per frame)
      |
egui::Ui methods (button, label, layout)
      |
[egui core]
   layout engine, input handling, z-order,
   texture atlas for fonts + images
      |
[Draw list]
   triangles + textures + clip rects
      |
[Integration layer]
   eframe      (native winit / web canvas)
   bevy_egui   (inside a Bevy game)
   egui_winit  (headless with winit)
   three-egui  (inside a Three.js-style 3D app)
      |
[Rendering]
   wgpu / glow / OpenGL / Metal / WebGPU

Self-Hosting & Configuration

use eframe::egui::{self, CentralPanel, Context};

struct App { name: String, age: u32 }

impl eframe::App for App {
    fn update(&mut self, ctx: &Context, _frame: &mut eframe::Frame) {
        CentralPanel::default().show(ctx, |ui| {
            ui.heading("Hello egui");
            ui.horizontal(|ui| {
                ui.label("Name:");
                ui.text_edit_singleline(&mut self.name);
            });
            ui.add(egui::Slider::new(&mut self.age, 0..=120).text("age"));
            if ui.button("Greet").clicked() {
                println!("Hello, {}!", self.name);
            }
            ui.collapsing("Advanced", |ui| {
                ui.label("More settings here");
            });
        });
    }
}

fn main() -> eframe::Result<()> {
    let app = App { name: "alice".into(), age: 30 };
    eframe::run_native("Demo", Default::default(), Box::new(|_cc| Ok(Box::new(app))))
}
# Native build
cargo run --release

# WASM build (browser)
# cargo install trunk
trunk serve --release
# or: wasm-pack build --target web

Key Features

  • Pure Rust — no C/C++ dependencies, cross-compiles easily
  • Universal target — same code runs native + WASM + mobile + embedded
  • Immediate mode — simple, state-free widget calls
  • Themeable — easy dark/light themes, custom color palettes
  • Plots + graphs — egui_plot for charts, Rerun for data visualization
  • Syntax highlighting — egui_extras + syntect support
  • Accessibility — keyboard navigation, screen reader hints (growing)
  • bevy_egui — drop-in for Bevy game engine UI/editor tools

Comparison with Similar Tools

Feature egui Dear ImGui Iced Tauri Slint
Language Rust C++ Rust Rust (HTML/CSS/JS) Rust/C++
Paradigm Immediate Immediate Elm-style retained Webview Declarative
Native + web Yes (same code) Via separate backends Native Native Native + embedded
Mobile Yes (growing) Partial No Yes Yes
Ecosystem size Large (Rust) Largest (games) Growing Large Small
Best For Rust apps + game tools C++ games Elm-style Rust apps Web-tech in native Embedded devices

FAQ

Q: egui vs Dear ImGui? A: Same paradigm (immediate mode). Dear ImGui dominates C++ game tools; egui is Rust-first with seamless WASM. Use Dear ImGui if your stack is C++; use egui if it's Rust.

Q: Can I build a full desktop app with egui? A: Yes — eframe provides a proper native window with menus, file dialogs, accessibility. Many shipping Rust apps (Rerun, Lapce parts, various game tools) use egui as their main UI.

Q: How small is the WASM bundle? A: ~1-3 MB compressed for a basic app. Larger than native JS frameworks, smaller than most Electron-alternative frameworks. wasm-opt + code-splitting help.

Q: Is egui ready for production? A: Yes. It's at 0.x semantic versioning but considered stable; breaking changes are rare and well-documented.

Sources

讨论

登录后参与讨论。
还没有评论,来写第一条吧。

相关资产