Skip to content

Instantly share code, notes, and snippets.

@dmezh
Created September 30, 2022 00:20
Show Gist options
  • Save dmezh/dbc2429762005660ec0dec287a0180a9 to your computer and use it in GitHub Desktop.
Save dmezh/dbc2429762005660ec0dec287a0180a9 to your computer and use it in GitHub Desktop.
#[derive(Debug)]
struct I;
struct J {
things: Vec<I>,
}
impl J {
pub fn new() -> Self {
J { things: Vec::new() }
}
pub fn add_to_things(&mut self, thing: I) -> &I {
let idx = self.things.len();
self.things.push(thing);
&self.things[idx]
}
}
fn main() {
let mut s = J::new();
let thing_inserted = s.add_to_things(I {}); // lifetime of this borrow extended to line 28?
let thing_inserted2 = s.add_to_things(I {});
println!("{:#?}", thing_inserted);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment