Thursday, September 5, 2024

My first UI based Rust application - A Skip Counter - created using iced UI library...

It's rightly said that when someone teaches another person, actually two people learn.

Here goes my contribution on Teacher's day.

Happy Teacher's day to all the Gurus of the Universe.

Here we go... 

Rust is primarily known for System programming. However, with growing popularity, eventually may be UI based application will be developed using Rust. Rust's UI libraries are still maturing in comparison to C++ (QT) and other languages, however, it's inbuilt memory safety issue may force global companies to consider Rust for UI based mission critical system like SCADA HMI and similar application areas.

Pros of Using Rust for UI Applications:

  1. Memory Safety: Rust's ownership model and memory safety features make it excellent for avoiding memory-related bugs, which is crucial for large-scale UI applications.
  2. Performance: Rust's performance is comparable to C and C++, which can be beneficial in resource-intensive UI applications.
  3. Concurrency: Rust's built-in concurrency model helps in writing fast and reliable multi-threaded applications, which is useful for interactive UIs.
  4. Cross-Platform: Libraries like Iced offer cross-platform capabilities, so you can build desktop applications for different operating systems.

Cons of Using Rust for UI Applications:

  1. Immature Ecosystem: Although libraries like Iced, and Slint are promising, they are still not as mature or feature-rich as UI toolkits available in other languages (e.g., Qt for C++, Flutter for Dart).
  2. Steep Learning Curve: The Rust programming model, particularly its strict borrow checker and ownership system, may pose a challenge for developers who are new to the language, making rapid UI prototyping slower.
  3. Fewer Libraries and Frameworks: While libraries like Iced, GTK exist for Rust, they lack the polish and comprehensive documentation of more established UI frameworks. Integrating features like animations or sophisticated layouts can be more difficult.
Yesterday I was developing a small UI based application called Skip Counter using the iced library for Rust. Here's what it looks like.



And here we go... the source code for this simple application.

use iced::{widget::{Button, Column, Text, TextInput},
Sandbox, Settings, Application, Command, executor,
Theme};
#[derive(Debug, Clone)]
enum Message {
SkipValueChanged(String),
Increment,
Reset,
}

struct SkipCounter {
count: i32,
skip_value: i32,
input_value: String,
}

impl SkipCounter {
fn new() -> Self {
SkipCounter {
count: 0,
skip_value: 1, // Default skip value
input_value: String::new(),
}
}
}
impl Application for SkipCounter {

type Executor = executor::Default; // Here you specify the type for Executor
type Flags = (); // Replace with your actual flags type
type Message = Message;

fn new(_flags: Self::Flags) -> (Self, Command<Self::Message>) {
(SkipCounter::new(), Command::none())
}

fn title(&self) -> String {
String::from("Skip Counter")
}

fn update(&mut self, message: Message) -> iced::Command<Message> {
match message {
Message::SkipValueChanged(value) => {
self.input_value = value.clone();
self.skip_value = value.parse::<i32>().unwrap_or(1);
Command::none()
}
Message::Increment => {
self.count += self.skip_value;
Command::none()
}
Message::Reset => {
self.count = 0;
Command::none()
}
}
}

fn view(&self) -> iced::Element<Message> {
let input = TextInput::new(
"Enter skip value",
&self.input_value,
//Message::SkipValueChanged
).on_input(Message::SkipValueChanged);

let increment_button = Button::new(Text::new("Increment"))
.on_press(Message::Increment);

let reset_button = Button::new(Text::new("Reset"))
.on_press(Message::Reset);

let count_text = Text::new(format!("Current Count: {}", self.count));

Column::new()
.padding(100)
.align_items(iced::Alignment::Center)
.push(input)
.push(increment_button)
.push(reset_button)
.push(count_text)
.into()
}

type Theme = iced::Theme;
}

fn main() -> iced::Result {
SkipCounter::run(iced::Settings::default())
}
 You must have got the point - Rust UI toolkits are still maturing. It will take time, but I am sure it will flourish in the near future.

No comments: