FAQ - Help and questions

How do I write a small desktop window with iced, and what are Message, update and view?

#iced#gui#desktop#elmpublished 0 replies0 views
ReplyWatch
Share
Hacker NewsRedditLinkedInE-mail

Short answer

An iced app is a state struct, a Message enum, an update function that changes the state when a message arrives, and a view function that builds the widgets from the state. Widgets never change the state themselves; they only send messages. A counter with two buttons passed cargo check with iced 0.13.

Checked with rustc 1.93.0, Windows 11, iced 0.13 (resolved by Cargo to the latest 0.13 release); `cargo check` only, the window was not opened. The iced API changed between versions: match the code to the version you use.

Frequently asked question, written and answered by the Ruststudio team. Replies are open: corrections are welcome.

#1

Answer#

iced uses the Elm architecture. There are three pieces:

  1. State: an ordinary struct (Counter).
  2. Message: an enum of everything that can happen (Increment, Decrement).
  3. update and view: update is the only place that changes the state; view is a function from the state to the widgets, called again after each change.
Cargo.tomltoml
[dependencies]
iced = "0.13"
src/main.rsrust
use iced::widget::{button, column, text};
use iced::Element;

#[derive(Default)]
struct Counter {
    value: i32,
}

#[derive(Debug, Clone)]
enum Message {
    Increment,
    Decrement,
}

impl Counter {
    // All changes happen here, in response to a message.
    fn update(&mut self, message: Message) {
        match message {
            Message::Increment => self.value += 1,
            Message::Decrement => self.value -= 1,
        }
    }

    // The view is a function of the state; it only describes the window.
    fn view(&self) -> Element<'_, Message> {
        column![
            button("+").on_press(Message::Increment),
            text(self.value).size(40),
            button("-").on_press(Message::Decrement),
        ]
        .padding(20)
        .into()
    }
}

fn main() -> iced::Result {
    iced::run("Counter", Counter::update, Counter::view)
}

This compiled with cargo check on Windows 11 using iced 0.13. It was not run.

The benefit of the structure: every change to the state goes through one match, so the list of things the program can do is the Message enum. Work that takes time (reading a file, a request) is started from update and comes back as another message.

Common mistakes#

  • Following an example written for another iced version. Entry points such as iced::run and the way the title is set have differed between releases; check the version of the example against your Cargo.toml.
  • Changing state inside view. view takes &self on purpose.

Can iced do more than buttons and text?#

Yes: the iced::widget module has more widgets, and the iced repository has an examples folder. This thread compiled only the counter.

Sources#

    QuoteReply
    Share
    Hacker NewsRedditLinkedInE-mail

    No replies yet