Dereferencing and comparing

fn main() {
let hello = "Hello".to_string();
let hello2 = "Hello";
let refer = &hello;
println!("{}" ,*refer==hello2)
}

let hello = String
let refer : &String
*refer = String value
and hello2 = string literal

How *refer and hello2 are comparable they are not even same type, one is String other is string literal?
(or am i missing something about dereferencing? Would be good if someone explain )

hello has type String.
hello2 has type &str.
refer has type &String.
*refer has type String.

The reason you can compare *refer == hello2, i.e. a String against a &str, is that String implements the trait PartialEq<&str>.

Types A and B can be compared if A implements PartialEq<B>. In this case, this is implemented for these particular types.

3 Likes

Short and clear thanks sir.

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.