Make sure you have Rust installed before starting. You’ll need Rust 1.92 or later.
What You’ll Build
A counter application that displays a number and two buttons to increment and decrement it. This demonstrates Iced’s core concepts: state management, message handling, and reactive UI updates.Add Iced dependency
Open
Cargo.toml and add Iced:Cargo.toml
The default features include the
wgpu renderer and thread-pool executor, which work for most applications. See Installation for other configurations.Define your application state
Open The
src/main.rs and define the state of your application. For a counter, you only need to track a single number:src/main.rs
Default trait allows Iced to initialize your application with Counter { value: 0 }.Define your messages
Messages represent the events your application can handle. For a counter, you need increment and decrement actions:
src/main.rs
Implement the update logic
The
update method handles messages and modifies your application state:src/main.rs
Implement the view logic
The The
view method describes how your application should look. It returns a layout of widgets that can produce messages:src/main.rs
column! macro creates a vertical layout. Each button produces a message when pressed, and the text widget displays the current counter value.Run your application
Add the main function to run your application:
src/main.rs
iced::run starts your application by taking your update and view functions. It automatically:- Creates a window
- Renders your UI
- Processes user interactions
- Calls your
updatemethod when buttons are clicked - Re-renders when state changes
Complete Code
Here’s the fullsrc/main.rs file:
src/main.rs
Understanding the Architecture
This counter demonstrates The Elm Architecture that Iced follows:- State (
Counter) - Holds your application data - Messages (
Message) - Represent events that can change state - Update logic (
update) - Handles messages and modifies state - View logic (
view) - Renders UI based on current state
- The button produces a
Message::IncrementorMessage::Decrement - Iced calls your
updatemethod with that message - Your
updatemethod modifies the state - Iced calls your
viewmethod to re-render the UI - You see the updated counter value
Next Steps
Now that you’ve built your first Iced application, explore more concepts:Architecture
Deep dive into The Elm Architecture and how Iced works
Widgets
Learn about available widgets and how to use them
Styling
Customize the appearance of your application
Tasks
Handle async operations with tasks
