1

I am trying to take a string from an input file and parse the information into a HashMap of structures:

use std::{fs::File, io::prelude::*};

pub struct Student {
    c_num: &'static str,
    cla: i32,
    ola: i32,
    quiz: i32,
    exam: i32,
    final_exam: i32,
}

impl Student {
    pub fn new(
        c_num: &'static str,
        cla: i32,
        ola: i32,
        quiz: i32,
        exam: i32,
        final_exam: i32,
    ) -> Student {
        Student {
            c_num: c_num,
            cla: cla,
            ola: ola,
            quiz: quiz,
            exam: exam,
            final_exam: final_exam,
        }
    }

    pub fn from_file(filename: String) -> Vec<Student> {
        let mut f = File::open(filename).expect("File not found");
        let mut contents = String::new();

        f.read_to_string(&mut contents);

        let mut students: Vec<Student> = Vec::new();

        for i in contents.lines().skip(1) {
            let mut bws = i.split_whitespace();
            let stdnt: Student = Student::new(
                bws.next().unwrap(),
                bws.next().unwrap().parse().unwrap(),
                bws.next().unwrap().parse().unwrap(),
                bws.next().unwrap().parse().unwrap(),
                bws.next().unwrap().parse().unwrap(),
                bws.next().unwrap().parse().unwrap(),
            );

            students.insert(0, stdnt);
        }

        students
    }
}

fn main() {}

When I try to compile, the compiler gives me this.

error[E0597]: `contents` does not live long enough
  --> src/main.rs:39:18
   |
39 |         for i in contents.lines().skip(1) {
   |                  ^^^^^^^^ borrowed value does not live long enough
...
54 |     }
   |     - borrowed value only lives until here
   |
   = note: borrowed value must be valid for the static lifetime...

Why does the contents variable need to live after the function returns?

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
  • Welcome to Stack Overflow! Please review how to create a [MCVE] and then [edit] your question to include it. If you remove all of the fields until you have the bare minimum to reproduce the problem, you are likely to answer your own question or have a more targeted question to ask. – Shepmaster Jun 05 '18 at 17:53
  • I believe your question is answered by the answers of [Why does my variable not live long enough?](https://stackoverflow.com/q/33286213/155423) and [Cannot split a string into string slices with explicit lifetimes because the string does not live long enough](https://stackoverflow.com/q/44641830/155423). If you disagree, please [edit] your question to explain the differences. Otherwise, we can mark this question as already answered. – Shepmaster Jun 05 '18 at 17:55

1 Answers1

5
c_num: &'static str,

This line says that Student has a member c_num, which is a reference to a string that lives forever.

The string you read out of the file does not live forever. It lives until the end of a loop iteration.

You probably want c_num to be of type String, so that the struct owns the value.

Sebastian Redl
  • 69,373
  • 8
  • 123
  • 157