When you call open(&args.path)
, where &args.path
is a &String
and open()
accepts a &str
and String
can dereference to a str
, the compiler will automatically dereference the &String
for you. This is one of the few implicit type coersions in Rust, and is also referred to as "auto deref".
This thread on auto deref even mentions how the compiler won't automatically dereference when doing pattern matching (i.e. your match
situation):
The equality check in your if-statement isn't doing any special conversions.
It's just combining impl PartialEq<str> for String
with the general impl<A, B> PartialEq<&B> for &A where B: PartialEq<A>
to infer that &String: PartialEq<&str>
, and therefore we can use ==
with a &String
on the left-hand side and &str
on the right.