Info
This post is auto-generated from RSS feed The Rust Programming Language Forum - Latest topics. Source: I don't understand this error: error: lifetime may not live long enough
I don't understand this error: error: lifetime may not live long enough
โ Rust ๐ 2025-08-29 ๐ค surdeus ๐๏ธ 3I am in the process of porting some (old) C++ code to rust. The code includes a main class (System), the contains HashMaps and Vectors of various other classes. The original code used pointers. For the most part most of this data structure is "static" -- it is read from a set of files and one section is ever persistently modified, although some of the other structures are updated during processing.
I have most of the data structure implemented and working, but I have hit a snag.
The original C++ code is here:
And my rust port is here:
The build log is here:
type or Compiling freight_car_forwarder v0.1.0 (/home/heller/RustProjects/freight_car_forwarder)
error: lifetime may not live long enough
--> src/system.rs:181:9
|
180 | pub fn IndustryByIndexMut(&mut self, i: usize) -> Option<&mut Industry> {
| ---------
| |
| let's call the lifetime of this reference `'1`
| has type `&mut system::System<'2>`
181 | self.industries.get_mut(&i)
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ associated function was supposed to return data with lifetime `'2` but it is returning data with lifetime `'1`
|
= note: requirement occurs because of a mutable reference to `Industry<'_>`
= note: mutable references are invariant over their type parameter
= help: see <https://doc.rust-lang.org/nomicon/subtyping.html> for more information about variance
error: could not compile `freight_car_forwarder` due to previous error
I think I need to specify lifetimes, but I am not sure where.
7 posts - 3 participants
๐ท๏ธ Rust_feed