Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use std::rc::Rc;
- use std::cell::RefCell;
- type NodePtr<T> = Option<Rc<RefCell<Node<T>>>>;
- struct Node<T> {
- val: T,
- next: NodePtr<T>,
- }
- impl<T> Drop for Node<T> {
- fn drop(&mut self) {
- println!("dropping");
- }
- }
- fn main() {
- let head = Rc::new(RefCell::new(Node {
- val: 10, next: None,
- }));
- let tail = Rc::new(RefCell::new(Node {
- val: 20, next: None,
- }));
- // mem leak caused by circular reference, same problem as shared_ptr in C++
- head.borrow_mut().next = Some(tail.clone());
- tail.borrow_mut().next = Some(head.clone());
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement