When should Borrow be implemented?

The Borrow and BorrowMut traits are not implemented on things like mutex guards or the RefCell borrow types. Why not? What are the conventions for when the trait should be implemented?

Context: tokio-rs/tokio#5625


I think the primary purpose of Borrow is for collections: You want to be able to store owned Strings as HashMap keys, but then use an &str to look up items without allocating; Borrow is the trait that encodes this relationship. Using a MutexGuard as a map key is a niche use, so I'm not surprised that the implementation never got written.

On the other hand, this doesn't explain the existence of BorrowMut, so I'm probably missing something.

1 Like

I found an IRLO topic where pretty much the same question was asked:

A link was shared to an issue that apparently raises concern with Borrow in connection with RefCell. Also it seems to be an issue that implementing Borrow now for these types (including MutexGuard) might be a breaking change? I couldn't make much sense of it TBH.


This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.