I'm implementing a binary search tree with Rc<RefCell<...>> (Playground)
use std::cell::RefCell;
use std::rc::Rc;
type RcRefBaseNode<T> = Rc<RefCell<BinarySearchTreeNode<T>>>;
type BaseNodeLink<T> = Option<RcRefBaseNode<T>>;
pub struct BinarySearchTreeNode<T: Ord> {
pub data: T,
left: BaseNodeLink<T>,
right: BaseNodeLink<T>,
}
impl <T: Ord> BinarySearchTreeNode<T> {
fn min(&self) -> &T {
self.left.as_ref().map_or(&self.data, |x| x.borrow().min())
}
}
An error occurred:
error[E0515]: cannot return value referencing temporary value
--> src/lib.rs:15:51
|
15 | self.left.as_ref().map_or(&self.data, |x| x.borrow().min())
| ----------^^^^^^
| |
| returns a value referencing data owned by the current function
| temporary value created here
If possible, I only want to change the content in min because I have implemented other methods for this structure.
Reference: Peek - Learn Rust With Entirely Too Many Linked Lists