Skip to lesson content
navan✳THE BUILD LABRUST
0/53 complete
RECIPE 06 / 8

A native desktop control room.

Build an egui/eframe desktop app around a separately tested Rust model.

BY NAVAN SAMI~35 MIN BUILDLOCAL PROJECT
Download this project ↓
eframe 0.37eguiNative desktop

01 The shape of the build

A desktop UI should reflect a model rather than burying every rule inside button handlers. This project keeps a bounded counter in a plain Rust library and connects it to an immediate-mode egui interface through eframe. UI code is rerun when a frame needs painting, so avoid performing slow filesystem or network operations directly in the rendering path. Model tests can run headlessly; opening the actual native window requires a graphical desktop and the platform graphics dependencies.

  1. 1

    Define a Counter whose methods preserve the range 0 through 100.

  2. 2

    Test the model independently of the windowing runtime.

  3. 3

    Implement eframe::App::ui with buttons that call the model.

  4. 4

    Use run_native to open the window on a machine with a desktop session.

02 Get it running

Install Rust stable and extract this project’s ZIP. Run these commands from the extracted desktop-counter folder. The full workbench keeps it under cookbook/desktop-counter.

terminal
cargo test --lib
cargo run
WHAT YOU SHOULD SEE

A native Build Lab / Counter window shows a bounded count, minus/plus buttons, and reset. The count never leaves 0–100.

First builds download dependencies. The included Cargo.lock files record the versions resolved during authoring. Use --locked with Cargo commands when you want to enforce those resolutions.

03 The complete source

Every source file below is also in the download. Start with the manifest, then follow the Rust entry point and any UI or test files.

Cargo.toml9 lines +
Cargo.toml
[package]
name = "desktop-counter"
version = "0.1.0"
edition = "2024"

[dependencies]
eframe = { version = "0.37", default-features = false, features = ["default_fonts", "glow", "x11", "wayland"] }

[workspace]
src/lib.rs34 lines +
src/lib.rs
#[derive(Default)]
pub struct Counter {
    value: u32,
}
impl Counter {
    pub fn value(&self) -> u32 {
        self.value
    }
    pub fn increment(&mut self) {
        self.value = (self.value + 1).min(100);
    }
    pub fn decrement(&mut self) {
        self.value = self.value.saturating_sub(1);
    }
    pub fn reset(&mut self) {
        self.value = 0;
    }
}
#[cfg(test)]
mod tests {
    use super::*;
    #[test]
    fn bounds() {
        let mut c = Counter::default();
        c.decrement();
        assert_eq!(c.value(), 0);
        for _ in 0..150 {
            c.increment();
        }
        assert_eq!(c.value(), 100);
        c.reset();
        assert_eq!(c.value(), 0);
    }
}
src/main.rs35 lines +
src/main.rs
use desktop_counter::Counter;
use eframe::egui;
#[derive(Default)]
struct CounterApp {
    counter: Counter,
}
impl eframe::App for CounterApp {
    fn ui(&mut self, ui: &mut egui::Ui, _frame: &mut eframe::Frame) {
        egui::CentralPanel::default().show(ui, |ui| {
            ui.heading("Small steps. Real state.");
            ui.label("The Build Lab / Rust desktop recipe");
            ui.separator();
            ui.heading(self.counter.value().to_string());
            ui.horizontal(|ui| {
                if ui.button("−").clicked() {
                    self.counter.decrement();
                }
                if ui.button("+").clicked() {
                    self.counter.increment();
                }
                if ui.button("Reset").clicked() {
                    self.counter.reset();
                }
            });
            ui.label("Range: 0–100. State lasts for this session.");
        });
    }
}
fn main() -> eframe::Result {
    eframe::run_native(
        "Build Lab / Counter",
        eframe::NativeOptions::default(),
        Box::new(|_| Ok(Box::new(CounterApp::default()))),
    )
}

04 Check the behavior

  • □cargo test --lib checks lower and upper bounds and reset.
  • □Repeated minus clicks at zero keep the value at zero.
  • □The native window responds to resize and closes normally on your desktop.

05 Know what you are building on

Requires a graphical desktop and platform graphics/windowing prerequisites.

The counter is in memory; closing the app resets it.

Move expensive work to a worker and report results back rather than blocking the UI.

MAKE IT YOUR OWN

Choose one boundary to improve. Write down the new behavior, add a test that demonstrates it, then change the implementation. Keep the original working path intact.

Built with curiosity. By Navan Sami. ↗Rust · Make the contract clear. Then build.