- {
- let c2 = c.clone();
- c.borrow_mut().register(move |val| {
- let mut guard = c2.borrow_mut();
- println!("Callback called with {}, ready to go for nested call.", val);
- guard.call(val+val)
- } );
- }
+ // This adds the cyclic closure, which refers to the `Callbacks` though `c2`.
+ let c2 = c.clone();
+ c.borrow_mut().register(move |val| {
+ // This `borrow_mut` won't fail because we are careful below to close the `RefCell`
+ // before triggering the cycle. You can see that this is the case because the log message
+ // below is printed.
+ let mut guard = c2.borrow_mut();
+ println!("Callback called with {}, ready to go for nested call.", val);
+ guard.call(val+val)
+ } );