Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

eGUI counter with label and button

  • button
  • clicked
  • label
use eframe::egui;

fn main() -> Result<(), eframe::Error> {
    let options = eframe::NativeOptions {
        viewport: egui::ViewportBuilder::default().with_inner_size([320.0, 240.0]),
        ..Default::default()
    };

    let mut counter = 0;

    eframe::run_simple_native("Rust Maven egui App", options, move |ctx, _frame| {
        egui::CentralPanel::default().show(ctx, |ui| {
            if ui.button("Increment").clicked() {
                counter += 1;
            }
            ui.label(format!("Count {counter}"));
        });
    })
}
  • Every time we click on the button, the code in the block gets executed incrementing the counter
  • Then the displayed label is refreshed.