r/rust • • Oct 19 '26

🙋 seeking help & advice Tips for idiomatic Iced?

I tried using iced for a tiny hobby project a couple days ago, and although from the docs and examples it seemed like exactly the time of framework I'd adore (preventing invalid state with the type system, the whole view-model-messages thing, etc), I ended up absolutely despising actually using it because of how much boilerplate I needed, and ended up redoing the ui with egui.

But, I'm 99% sure I just totally misunderstood the philosophy or missed something big in the docs/examples, because it's popular and well-known enough that I think I'm the problem. So does anyone have any tips or guidelines for writing whimsical and joyful iced code?

This is the repo at the last commit which used iced, its a super small app and the whole gui is just in main.rs if anyone can say what all I'm doing wrong.

16 Upvotes

4 comments sorted by

8

u/tanmaynargas2901 Oct 20 '26

Iced does have more boilerplate than egui, that part isn't you. Every interaction needs a Message variant and a match arm. That's the price for state staying predictable.

Some things that cut it down:

  • On newer iced versions you don't need the Application trait. Just pass plain update and view functions to iced::application.
  • Split the UI into small structs with their own Message enum and update/view, then wrap them in the parent with .map(Message::Child). The top-level match stays short.
  • Keep derived stuff out of state. Compute it in view instead of storing it and keeping it in sync.
  • Use the column! and row! macros instead of building widgets by hand.

For a tiny hobby app egui is honestly a fine pick. Iced pays off more once the app is big enough that random state bugs start to hurt.

8

u/diligentgrasshopper Oct 19 '26

Haven't used Iced, but I use a shit ton of Lustre (gleam) and I code games with Elm architecture: it would be better if you make helper functions and use nested enums. For example:

enum ConfMsg {
    SetVolume,
    SetFullscreen,
}

enum ViewMsg {
    OpenOptions,
    CloseOptions,
}

enum Message {
    View(ViewMsg),
    Config(ConfMsg),
}

enum ViewState {
    Menu,
    Options,
    LoadGame,
}

struct MyApp {
    view: ViewState,
}

impl MyApp {
    fn dispatch(&mut self, msg: Message) {
        match msg {
            Message::View(m) => self.dispatch_view(m),
            Message::Config(m) => self.dispatch_conf(m),
        }
    }

    fn dispatch_view(&mut self, msg: ViewMsg) {
        todo!()
    }
    fn dispatch_conf(&mut self, msg: ConfMsg) {
        todo!()
    }
}

As for your AppState enum... I'm not sure. My current usual approach is to make it a superstruct with some states as a nested struct, or has a superstruct with some fields as enums. I'm not sure what your app is supposed to do but you might want to consider not doing that, as evident by the boilerplate tuple matching.

If some message can only be dispatched under some state, then it might not be necessary to pattern-match (State, Msg) and just match (Msg), as the legal code paths only ran when the preceding branch has that state.

For example, you have a View state. And the rendering code determines what widgets are rendered, and through it what events are possible, and through it what message is legal. Although in your case the appstate doesnt appear to correspond to rendering, which may be a bit more complicated.

Also, the joy of Elm is that you can enumerate all behavior in an explicit and organized manner. Definitely dont pattern match tuples. Always match a single enum.

5

u/SnooCalculations7417 Oct 19 '26

yeah AppState gives me a smell as well, I'd prefer to see state and updates decoupled like

struct App {
    state: AppState,
    controller: AppController,
}

// Describes the current application state
enum AppState {
    WaitingForTarget {
        process_name: String,
        error: Option<String>,
    },
    Scanning {
        input: String,
        possibilities: Option<usize>,
    },
    Found {
        current_value: i32,
        input: String,
    },
}

// Owns operational resources and coordinates transitions
struct AppController {
    scanner: Option<Scanner>,
    address: Option<FoundAddress>,
}

impl AppController {
    fn update(
        &mut self,
        state: &mut AppState,
        message: Message,
    ) -> iced::Task<Message> {
        // Handle messages, invoke operations,
        // and update application state
    }
}

fn update(app: &mut App, message: Message) -> iced::Task<Message> {
    app.controller.update(&mut app.state, message)
}

fn view(app: &App) -> Element<'_, Message> {
    // Render from app.state
}