How to return a pointer to owned value that “does not live long enough”?
问题 I have the following code: struct Node { id: uint } struct Graph { nodes: Vec<Node> } impl Graph { fn new() -> Graph { return Graph { nodes: Vec::new() }; } fn create_node(&mut self) -> &Node { let index = self.nodes.len(); let node = Node { id: index }; self.nodes.push(node); // return &node; // error: `node` does not live long enough return &self.nodes[index]; // ...but this work fine } } The idea is that the graph creates a new node and "lends" it to someone who calls the method. But I can