Understanding Borrow and ToOwned

To summarize the answers to the original post:

Question #1:

Answer #1:

Question #2:

Answer #2:

To give an example: String implements both Borrow<String> and Borrow<str>, so I can borrow it either as String or as str. And Borrow will assure that Ord behaves the same on the respective borrowed type (unlike AsRef).

This reminds me of the discussion in What is the difference between as_ref() & '&'. I would say that it's actually the main point of Borrow::borrow to be generic and to allow borrowing different types. Otherwise, we could just use &.

1 Like