5

I have a structure with a field defined as follows:

log_str: RefCell<String>

I performed various calls to borrow_mut() to call push_str(.) on the field. At the end, I'm assessing its value using:

assert_eq!(os.log_str.borrow(), "<expected value>");

Nonetheless, the line of the assert raises a compile-time error with the message:

error[E0369]: binary operation == cannot be applied to type std::cell::Ref<'_, std::string::String>

I understand why the error is happening, since the compiler even hints:

an implementation of std::cmp::PartialEq might be missing for std::cell::Ref<'_, std::string::String>

My question is: how should I compare the value enclosed in a RefCell<T> (typically in this case, comparing the enclosed string with an expected value).

Thanks !

Jämes
  • 6,945
  • 4
  • 40
  • 56

1 Answers1

4

You want to de-reference the borrowed value:

assert_eq!(*os.log_str.borrow(), "<expected value>");
edwardw
  • 12,652
  • 3
  • 40
  • 51
  • Alright! `Ref<.>` can effectively be dereferenced (I guess thanks to the implementation of `Deref` trait). Thanks! – Jämes Aug 22 '19 at 16:34