library/boxs/
linked_list.rs

1use std::{cell::RefCell, rc::Rc};
2
3use log::info;
4use List::{Cons, Nil};
5
6#[derive(Debug)]
7enum List {
8    Cons(Rc<RefCell<i32>>, Rc<List>),
9    Nil,
10}
11
12pub fn list_test() {
13    let val = Rc::new(RefCell::new(0));
14    let a = Rc::new(Cons(
15        Rc::new(RefCell::new(1)),
16        Rc::new(Cons(Rc::clone(&val), Rc::new(Nil))),
17    ));
18    info!("count after creating a = {}", Rc::strong_count(&a));
19    let b = Cons(Rc::new(RefCell::new(2)), Rc::clone(&a));
20    info!("count after creating a = {}", Rc::strong_count(&a));
21    let c = Cons(Rc::new(RefCell::new(3)), Rc::clone(&a));
22    info!("count after creating a = {}", Rc::strong_count(&a));
23
24    *val.borrow_mut() += 10;
25    info!("muted: a: {:?}, b: {:?}, c: {:?}", a, b, c);
26}