Spot the bug in the callback registry

from Cell and RefCell
Rust 1.98 advanced 4 min 1 issue to find

Find why a callback that subscribes another callback can panic.

Rust
use std::cell::RefCell;
use std::rc::Rc;

struct Registry {
    callbacks: RefCell<Vec<Rc<dyn Fn(&Registry)>>>,
}

impl Registry {
    fn subscribe(&self, callback: Rc<dyn Fn(&Registry)>) {
        self.callbacks.borrow_mut().push(callback);
    }

    fn notify(&self) {
        for callback in self.callbacks.borrow().iter() {
            callback(self);
        }
    }
}
Open in playground
Report an error