This is mainly due to the push_item function which I see may have extra steps.
use std::{cell::RefCell, rc::Rc};
#[derive(Debug)]
struct Value {
value: i32,
}
#[derive(Debug)]
struct List {
index: usize,
list: Rc<RefCell<Vec<Value>>>,
}
fn push_item(
ptr: &Rc<RefCell<List>>,
val: i32,
) {
let ptr_ref = ptr.as_ref().borrow();
let list_rc = Rc::clone(&ptr_ref.list);
let mut list_ref = list_rc.as_ref().borrow_mut();
list_ref.push(Value { value: val });
}
fn main() {
println!("Hello, world!");
let list = List {
index: 0,
list: Rc::new(RefCell::new(vec![])),
};
let rclist = Rc::new(RefCell::new(list));
let mut val = 0;
loop {
if val >= 10 {
break;
}
push_item(&rclist, val);
val += 1;
}
println!("value (p): {:#?}", rclist);
}