Clarify RefCell and take()


I’m looking at this example from the wasm_bindgen repo:

And having trouble understanding this line: let _ = f.borrow_mut().take();

What is take() being called on? If I look at the docs, I don’t see take() on RefCell (which I think is what borrow_mut() is returning)

More generally - is there a way on the docs site to see all the methods implemented for a given struct/enum, including those that are due to satisfying the various Traits?


take is method of Option.

The line is the same as;

*f.borrow_mut() = None;

I think the complication your having understanding the code is down to auto dereferencing. When dealing with generics it becomes impossible to have all methods generated staticly. Even tool (rls/racer) that give more dynamic results are still struggling to give every method when auto-completing code.

1 Like

Oh… right on… wow that does become tricky, of knowing like which type you really end up at (due to the Deref chain)

Any tips for getting a better intuition about that? :slight_smile:


This is why whether to implement Deref should be thought about with care. Idiomatically, it is only used in few places:

  • owned types that have a borrowed equivalent, like String <-> str
  • smart pointers, like Rc or Box
  • “transparent wrapper” types that are necessary to guarantee safety, like MutexGuard or Ref for RefCells
1 Like

One reason I continue to use IntelliJ Community Edition (I can’t afford CLion.) even though I have to debug with VSCode is that the IDE shows me the inferred types. In your example, I would write

let foo = f.borrow_mut();

and IntelliJ would show me that foo was of type RefMut<MyType>, where MyType is what you put in the RefCell. Autocomplete shows me methods of MyType if I put a “.” following the (), which tells me about the auto dereferencing.