#Help with borrowing

11 messages · Page 1 of 1 (latest)

polar dew
#

I have the following function

pub fn map_debug_level(&self) -> log::LevelFilter {
    let debug_level = match self
        .debug
        .as_ref()
        .unwrap_or(|| -> &HashMap<String, String> {
            let mut map = HashMap::new();
            map.insert("LEVEL".to_string(), "debug".to_string());
            map
        }())
        .get("LEVEL")
        .unwrap()
        .as_str()
    {
        "trace" => log::LevelFilter::Trace,
        "debug" => log::LevelFilter::Debug,
        "info" => log::LevelFilter::Info,
        "warn" => log::LevelFilter::Warn,
        "error" => log::LevelFilter::Error,
        "off" => log::LevelFilter::Off,
        _ => log::LevelFilter::Debug,
    };
    debug_level
}

The error:

mismatched types
expected reference `&std::collections::HashMap<std::string::String, std::string::String>`
      found struct `std::collections::HashMap<std::string::String, std::string::String>`

The hint is to borrow, of course

&map

However that yeilds the following error:

cannot return reference to local variable `map`
returns a reference to data owned by the current function

No idea what i should do, i am still pretty new to borrowing and lifetimes in rust 🙂

#

Nvm - i did a dumb and i was trying to catch the wrong unwrap xD

pub fn map_debug_level(&self) -> log::LevelFilter {
    let debug_level = match self
        .debug
        .as_ref()
        .unwrap()
        .get("LEVEL")
        .unwrap_or(&String::from("off"))
        .as_str()
    {
        "trace" => log::LevelFilter::Trace,
        "debug" => log::LevelFilter::Debug,
        "info" => log::LevelFilter::Info,
        "warn" => log::LevelFilter::Warn,
        "error" => log::LevelFilter::Error,
        "off" => log::LevelFilter::Off,
        _ => log::LevelFilter::Debug,
    };
    debug_level
}

That is the smarter way to do it 😅

#

Though, if there is a better way to do this logging shenanigans i am all ears 🙂

viscid steppe
#

If you needed the HashMap, you could use Cow:

let debug_level = match self
    .debug
    .as_ref()
    .map(Cow::Borrowed)
    .unwrap_or(|| -> Cow<HashMap<String, String>> {
        let mut map = HashMap::new();
        map.insert("LEVEL".to_string(), "debug".to_string());
        Cow::Owned(map)
    }())
    .get("LEVEL")
    .unwrap()
    .as_str()
```However, if you just unwrap later you don't need to do that.
```rs
let debug_level = match self
    .debug
    .as_ref()
    .map(|hashmap| hashmap.get("LEVEL").unwrap().as_str())
    .unwrap_or("off")
viscid steppe
polar dew
viscid steppe
#

The best way would be to have a HashMap<String, LevelFilter> to begin with but idk if that'll work with the rest of the project

polar dew
#

Still gotta get used to all of the methods

polar dew
#

i am not sure, could be wrong, that i can read the string from the config file as a level filter to the map, so i made function to "map" specific strings to the enum.

#

Thank you for the help 🙂