Code structure in a GTK application


Hey guys, i want to write my first bigger application in rust, but now i have a small “problem”. I don’t know how I should structure the modules or the code.

I have three main components:

  • AudioPlayer
  • Client
  • Library

The three structs must be accessible from everywhere. Currently I have a “App” struct, which contains the three structs, and some UI structs (window, pages, etc.).

Now I have some GtkListBoxRows which contain some buttons like “Play”, “Add to library” or “Edit”.
How i should access, for example, the AudioPlayer struct, in my “” module?

There are several ways, but many of them require a lot of boilerplate code. What do you think, what is the best way to do this in Rust?

In other languages I would do something like this:

class App{
  public static AudioPlayer player;
  public static Library library;

class ListBoxRow{
  button.clicked.connect(... App.library.do_something() ...)

I hope you understand what I mean.
I am looking forward to your ideas :slight_smile:


I believe a fairly common approach is to put your shared state inside an Rc<RefCell<...>>, clone() it and pass the clones to the places that need access to it. So for example:

struct App {
    player: Rc<RefCell<AudioPlayer>>,
    library: Rc<RefCell<Library>>,