2

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?

C. Dautermann
  • 109
  • 1
  • 10

1 Answers1

0

I don't know why storing the variable into a file is slow and stupid. It's the standard as far as I know, you typically access that file less that once per minute. If you mean communicating between threads or functions, you definitely should redesign the app. Use mpsc.

sunkeet
  • 1
  • 3
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Feb 22 '23 at 13:03