A standard Iced application lifecycle is very similar to how Elm does it.
Here's a break down:
- The
viewrenders widgets with the current state - The user interacts with these widgets (e.g. a button)
- The button dispatches a message
- The message is processed by
updateand mutates the state - Repeat...
In practical terms, considering our application below:
- The
viewis rendering a text and a button in a column layout - The user clicks the button, which listens for an event (
on_press) - The button dispatches the message (
Message::ButtonPressed) - The message is matched on the
updatefunction, and the counter is incremented by 1 - Repeat...
use iced::widget::{button, column, text};
fn main() -> iced::Result {
iced::run("My App", MyApp::update, MyApp::view)
}
#[derive(Debug, Clone)]
enum Message {
ButtonPressed,
}
#[derive(Default)]
struct MyApp {
counter: usize,
}
impl MyApp {
fn update(&mut self, message: Message) {
match message {
Message::ButtonPressed => self.counter += 1,
}
}
fn view(&self) -> iced::Element<Message> {
column![
text(self.counter),
button("Increase").on_press(Message::ButtonPressed),
]
.into()
}
}➡️ Next: Adding Widgets
📘 Back: Table of contents
