I'm trying to create a HashMap
from the filesystem (using .bashrc
in this example), but it doesn't compile due to lifetime problems. I can't figure out what the issue is, though. What am I doing wrong here?
use std::{collections::HashMap, fs};
#[derive(Debug)]
struct Aliases<'a> {
pub aliases: HashMap<&'a str, &'a str>
}
impl<'a> Aliases<'a> {
fn from_bashrc() -> Self {
let data = fs::read(".bashrc").unwrap();
let data = std::str::from_utf8(&data).unwrap();
let mut map = HashMap::new();
for line in data.split('\n') {
// this is a bad way to find aliases, just an example
let parts: Vec<&str> = line.split('=').collect();
if parts.len() == 2 {
map.insert(parts[0], parts[1]);
}
}
Self {
aliases: map,
}
}
}
fn main() {
let aliases = Aliases::from_bashrc();
println!("{:#?}", aliases);
}