Why recursive auto deref not work with `from(&T)`

Here is my try:

There are not very important because compiler will tell me there are a problem when I miss some & or * but I just want to know why. Maybe some rule I'm missing.

Auto-deref only happens if the output type of the Deref is already uniquely determined by some other mechanism. Hence, when generics gets involved, it typically does not happen automatically.

You can explicitly add the first Deref step: String::from(&*a);

2 Likes

To say the same thing as alice but from the other side, it will work if you say which From you're trying to use, via

<String as From<&str>>::from(&a);
1 Like

So fast and really make sense. Thank you all!

You could use dot syntax in this particular situation because that's guaranteed to call deref as often as necessary.

let _: String = a.into();
1 Like

Terminology: what's happening in String::from(&a) is deref coercion, not autoderef, which is what happens when you use a method or field with . syntax. See (stackoverflow) What is the relation between auto-dereferencing and deref coercion?

2 Likes

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.