Skip to content

Instantly share code, notes, and snippets.

@dmezh
Last active September 30, 2022 00:33
Show Gist options
  • Save dmezh/3359b72c40dfaf943337cfb46c70f67b to your computer and use it in GitHub Desktop.
Save dmezh/3359b72c40dfaf943337cfb46c70f67b 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]
}
pub fn get_thing(&self) -> &I {
return &self.things[0];
}
}
fn main() {
let mut s = J::new();
let thing_inserted = s.add_to_things(I {}); // lifetime of this borrow extended to line 32?
let thing_back = s.get_thing(); // does not compile
println!("{:#?}", thing_inserted);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment