Skip to content

Instantly share code, notes, and snippets.

@kellpossible
Last active September 27, 2016 07:28
Show Gist options
  • Save kellpossible/99d711c09d8fb90c4bb20abb54ba4fb9 to your computer and use it in GitHub Desktop.
Save kellpossible/99d711c09d8fb90c4bb20abb54ba4fb9 to your computer and use it in GitHub Desktop.
rust lifetime error
pub struct Database<'a> {
pub fixes: Vec<Waypoint<'a>>,
pub countries: HashMap<String, Country>,
}
impl<'a> Database<'a> {
pub fn new(navdata_dir: PathBuf, resources_dir: PathBuf) -> Database<'a> {
let countries_path = resources_dir.join("icao_countries.txt");
let countries_path = countries_path.to_str().unwrap();
let waypoints_path = navdata_dir.join("waypoints.txt");
let waypoints_path = waypoints_path.to_str().unwrap();
let mut db = Database {
countries: HashMap::new(),
fixes: Vec::new()
};
db.read_countries(countries_path);
db.read_fixes(waypoints_path);
return db;
}
...
}
@kellpossible
Copy link
Author

kellpossible commented Sep 27, 2016

I'm getting this error:

error: `db` does not live long enough
  --> database.rs:35:6
   |
35 |        db.read_fixes(waypoints_path);
   |        ^^ does not live long enough
...
38 |    }
   |    - borrowed value only lives until here
   |
note: borrowed value must be valid for the lifetime 'a as defined on the block at 22:74...
  --> database.rs:22:75
   |
22 |    pub fn new(navdata_dir: PathBuf, resources_dir: PathBuf) -> Database<'a> {
   |                                                                             ^

error: aborting due to previous error

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment