Idiomatic way to define key-value pairs globally?

Hi Rustaceans, i'm developing a GTK+ 3 application and need to set and keep track of key-value pairs globally in a helper module. So, this is what i came up with initially:

use std::env;
use std::string::String;

pub fn get_env(key: String) -> String {
    let val = env::var(key).unwrap();
    val;
}

pub fn set_env(key: &String, val: &String) {
    env::set_var(key, val);
}

However, this feels hacky and i don't want to mess up with the environment variables this way. So, is there a better approach for keeping the global vars somewhere? Maybe Gtk-rs module provides an helper function or something for achieving this? Thanks in advance!

You can use a Mutex<HashMap> in lazy_static.

BTW: Don't use &String type. It means "I want string type that I can grow in length, but I want read-only access to it that prevents it from ever growing", which doesn't make sense. &str is the correct type for string arguments.

3 Likes

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.