In this post, we will see how to resolve why does rust allow comparison between &String and string slice?
Question:
Best Answer:
BecausePartialEq<str>
is implemented for String
, so you can compare String
with str
, and there is a blanket implementation <A: PartialEq<B>> PartialEq<&B> for &A
, so you can also compare &String
with &str
.This is not related to deref coercion, as it does not apply to operators.
If you have better answer, please add a comment about this, thank you!
Source: Stackoverflow.com