Why am I getting ref to wrong type?

Hi all. I feel like I’m missing something obvious here. I have two structs which depend on each other:

struct A {
    b: Rc<RefCell<B>>,
}
struct B {
    a: Weak<RefCell<A>>,
}

and a new function to construct them:

impl A {
    pub fn new() -> Rc<RefCell<A>> {
        let b = Rc::new(RefCell::new(B::new()));
        let a = Rc::new(RefCell::new(A {b: b.clone()}));

        // in B, fn set_a(&mut self, a: Weak<A>)
        b.borrow_mut().set_a(Rc::downgrade(&a.clone()));

        drop(b);
        a
    }
}

In the line where I call Rc::downgrade, the compiler complains that it’s expected a reference to an Rc, but it’s getting a reference to the contained RefCell. Why am I not getting a reference to the Rc itself?

Hi, that’s not the error I got:

expected type `&std::rc::Rc<A>`
   found type `&std::rc::Rc<std::cell::RefCell<A>>`

I think you forgot the RefCell in set_a:

fn set_a(&mut self, a: Weak<RefCell<A>>)

If you are going to borrow a value, don’t also clone it.

b.borrow_mut().set_a(Rc::downgrade(&a));

Second, your signature for set_a is wrong, it should be

fn set_a(&mut self, a: Weak<RefCell<A>>)

Yes, that was the problem, the signature on the set call was wrong. Thanks!!