rustinterior-mutability

Comparing value enclosed in RefCell<T>


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 !


Solution

  • You want to de-reference the borrowed value:

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