I do not understand the error message I am getting for the following code.
use std::collections::HashMap; use std::rc::Weak; struct ThingPool { thing_map: HashMap<String, Thing>, } impl ThingPool { pub fn get(&self, key: &str) -> Option<&Thing> { self.thing_map.get(key) } } struct Thing { pool: Weak<RefCell<ThingPool>>, key: String, parent_key: String, } impl Thing { pub fn parent(&self) -> Option<&Thing> { let pool = &*self .pool .upgrade() .expect("FATAL: ThingPool no longer exists") .borrow(); pool.get(&self.parent_key) } } fn main() { // ... } I have a recursive data structure of Things, and I am trying to write a method that looks up the parent of a particular Thing in the ThingPool based on its key. The error message I am getting is:
--> src/main.rs:30:9 | 24 | let pool = &*self | ______________________- 25 | | .pool 26 | | .upgrade() 27 | | .expect("FATAL: ThingPool no longer exists") 28 | | .borrow(); | |_____________________- temporary value created here 29 | 30 | pool.get(&self.parent_key) | ^^^^^^^^^^^^^^^^^^^^^^^^^^ returns a value referencing data owned by the current function I understand that I cannot return a reference to a local value, but I do not understand what the local value is in this case. What is the "data owned by the current function"? The value I am returning is a value of the HashMap in the ThingPool. I am not copying the pool and therefore the HashMap and therewith the value should not be local to the function. What am I missing?