There are some examples of OO rust in the documentation here for instance: https://gtk-rs.org/gtk-rs-core/stable/latest/docs/glib/subcl...
Or you can look at the repository linked in this HN submission as a practical example...
---
Outside of the OO-gtk world, there's been quite a bit of experimentation with non-OO gui systems in rust, but I don't think there's a settled "best design" yet. Many designs look something like the following
- model: rust struct describing your data
- view: function mapping your data to widgets, widgets understanding how to layout, render, and issue commands
- commands: events in view generate simple command structs
- update fn: iterates over commands, updating model
With the framework passing input to widgets, and taking commands from the widget and passing them to the update fn, and then calling view to update widgets. Potentially intelligently to avoid redundant work (e.g. not re-rendering widgets that haven't changed).
The bigger constraint driving this than "not-OO" is "not mutably aliasing data". You probably don't want to try and have your update functions take a mutable reference to your model, because you will end up fighting the borrow checker. (On the other hand, I think egui does manage to pass mutable references to the model to callbacks, so maybe this is just a reflection of the rust gui's I've personally experimented with)
So in a slightly imaginary reasonably representative gui framework, I might have
struct Model {
chickens_can_fly: bool
}
enum Command {
ToggleChickensCanFly
}
fn view(model: &Model) -> impl Widget {
CheckBox {
checked: model.chickens_can_fly,
on_change: || Command::ToggleChickensCanFly
}
}
fn update(model: &mut Model, command: Command) {
match command {
Command::ToggleChickensCanFly =>
model.chickens_can_fly = !model.chickens_can_fly
}
}