Skip to main content

Model

Trait Model 

Source
pub trait Model: Sized {
    type Message: From<Event> + Send + 'static;

    // Required methods
    fn update(&mut self, msg: Self::Message) -> Cmd<Self::Message>;
    fn view(&self, frame: &mut Frame<'_>);

    // Provided methods
    fn init(&mut self) -> Cmd<Self::Message> { ... }
    fn subscriptions(&self) -> Vec<Box<dyn Subscription<Self::Message>>> { ... }
}
Expand description

The Model trait defines application state and behavior.

Implementations define how the application responds to events and renders its current state.

Required Associated Types§

Source

type Message: From<Event> + Send + 'static

The message type for this model.

Messages represent actions that update the model state. Must be convertible from terminal events.

Required Methods§

Source

fn update(&mut self, msg: Self::Message) -> Cmd<Self::Message>

Update the model in response to a message.

This is the core state transition function. Returns commands for any side effects that should be executed.

Source

fn view(&self, frame: &mut Frame<'_>)

Render the current state to a frame.

Called after updates when the UI needs to be redrawn.

Provided Methods§

Source

fn init(&mut self) -> Cmd<Self::Message>

Initialize the model with startup commands.

Called once when the program starts. Return commands to execute initial side effects like loading data.

Source

fn subscriptions(&self) -> Vec<Box<dyn Subscription<Self::Message>>>

Declare active subscriptions.

Called after each update(). The runtime compares the returned set (by SubId) against currently running subscriptions and starts/stops as needed. Returning an empty vec stops all subscriptions.

§Default

Returns an empty vec (no subscriptions).

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§