Lifetime issue while using string::new


#1

Hello ,

I am trying to do something like this

#[macro_use]
extern crate lazy_static;

use std::collections::HashMap;
use std::fs::File;
use std::io::Read;

lazy_static! {
    static ref HASHMAP: HashMap<&'static str, &'static str> = {
        let mut m = HashMap::new();
    
        let mut file = File::open("conf.ini").expect("opening file");
    let mut text = String::new();
	
	file.read_to_string(&mut text).expect("reading file");

	for line in text.lines() {
    println!("{}", line);
    let v: Vec<&str> = line.split('=').collect();
    m.insert(v[0], v[1]);
	}
	
    m
};
static ref COUNT: usize = HASHMAP.len();
static ref NUMBER: u32 = times_two(21);
}

fn times_two(n: u32) -> u32 { n * 2 }

fn main() {
    println!("The map has {} entries.", *COUNT);
    println!("A expensive calculation on a static results in: {}.", *NUMBER);
 }

I am getting below lifetime error , any idea on solving this

error[E0597]: text does not live long enough
–> src/main.rs:20:15
|
20 | for line in text.lines() {
| ^^^^ does not live long enough

27 | };
| - borrowed value only lives until here
|
= note: borrowed value must be valid for the static lifetime…

error: aborting due to previous error

error: Could not compile mio.


#2

The issue is you’re saying the HASHMAP contains strings which last the entire lifetime of the program (i.e. they were compiled in as string constants), but you then read a file into a temporary string (which gets destroyed at the end of the lazy_static “initializer block”) and put slices of that in the HASHMAP. A reference to some temporary variable doesn’t last for the lifetime of the program, which is why you’re getting these lifetime errors.

You almost certainly want to change the definition of your HASHMAP so it owns the strings it contains, instead of borrowing them, i.e. change its definition to HashMap<String, String>.


As a side note, 99% of the time you don’t need to make things global using lazy_static!(), it’s considered a bit of a code smell and tends to make your program harder to maintain. Just store the hashmap as a local variable in main(), passing around references to it if other bits of code need access.


#3

Makes sense. Thank you for the suggestion. I got a clarity as in why this was giving a problem.