Answer#
egui is an immediate-mode library: every frame your code says "a heading, a text box, a button" and egui draws it and reports what happened (was the button clicked?). Your own struct holds the state, so a click is just an if in the code that draws the button. eframe is the crate that opens the window and runs the loop.
[dependencies]
eframe = { version = "0.35", default-features = false, features = ["default_fonts", "glow"] }use eframe::egui;
#[derive(Default)]
struct App {
name: String,
clicks: u32,
}
impl eframe::App for App {
// Called for every frame: describe the whole window again from the current state.
fn ui(&mut self, ui: &mut egui::Ui, _frame: &mut eframe::Frame) {
egui::CentralPanel::default().show(ui, |ui| {
ui.heading("Hello");
ui.horizontal(|ui| {
ui.label("Name:");
ui.text_edit_singleline(&mut self.name);
});
if ui.button("Greet").clicked() {
self.clicks += 1;
}
ui.label(format!("Hello, {}! ({} clicks)", self.name, self.clicks));
});
}
}
fn main() -> eframe::Result {
let options = eframe::NativeOptions::default();
eframe::run_native("Hello", options, Box::new(|_cc| Ok(Box::new(App::default()))))
}The program compiled with cargo check on Windows 11. The window itself was not opened for this thread.
How it works#
run_nativetakes the window title, options, and a closure that builds your app once. After that eframe callsuiagain whenever something needs redrawing.ui.text_edit_singleline(&mut self.name)edits the string in place; there is no "text changed" callback to wire up.- The
default-features = falseline keeps the dependency list small; theglowfeature selects the OpenGL renderer anddefault_fontsbrings the fonts egui needs to show text. Other renderer features exist; see the eframe documentation.
Common mistakes#
- Doing slow work inside
ui. It runs every frame, so a slow call freezes the window. See the thread on background work. - Expecting the method signature to stay fixed between eframe versions. The trait changed over time; match the examples to the version in your
Cargo.toml.
Related questions#
Why does my egui app use CPU when nothing happens?#
egui redraws when it gets input or when you ask for a repaint. If you call ctx.request_repaint() every frame, it redraws continuously.
Sources#
- eframe on docs.rs: https://docs.rs/eframe/0.35.0/eframe/ (read 2026-12-05)
- egui README: https://github.com/emilk/egui (read 2026-12-05)