how to share information globally in gtk-rs Application

Viewed 41

I am trying to build an Application in Rust with gtk-rs. When the user creates a new project the path to the project root needs to be stored in some way so that it is accessible to all of the application. Things I have tried to solve this:

  • Create a mutable static String (unsafe and thus probably not a viable solution)
  • Store the variable into a file which is slow and stupid for obvious reasons. Works though...
  • Use lazy_static! as suggested in This Post. But it also says that this is generally not the preferred way. So I tried the next thing.
  • Create a mut project_root: &mut String and pass it to any function that needs it. Now the issue with this is, when I need to call new_proj_menu_item.connect_activate(move |_| new_project_listener::add_listener(&file_tree_view, project_root)); to set up the listener for the menu Item, the compiler tells me: "project_root has an anonymous lifetime '_ but it needs to satisfy a 'static lifetime requirement E0759 ...is captured here... Note: ...and is required to live as long as 'static here" Which I do not fully understand but seems to be related to the first thing I tried (project_root is the String).

Now my question is: How would I go about storing a variable like this so that it is accessible from the entire project? Is there maybe a GTK internal way that I am unaware of?

0 Answers
Related