I'm trying to make a function that accepts an AsRef trait to make it easier for myself to call it with either a String or a &str or basically anything on that form. But I have a problem with the compiler telling me I have the wrong type.
In the following code the compiler is giving me an error.
What is going on here in the compiler? I know it's something to do with the strings conversion I am doing, but I cannot wrap my head around it. Does it hardlock the type of T when I call the .as_ref() function in the mapping?
Alternatively, you can change the call to no longer use &[&str], &Option<T>, but e.g. &[&str], &Option<&str> instead, by receiver(&strings, &chars.as_ref().map(T::as_ref))
Yet another option: Call it with &[&T], &Option<&T> by
For this sort of type signature, I often prefer impl syntax. It reduces the visual distance between the parameter name and the type it actually takes. This is equivalent to the code above, for instance:
I'm coming from other languages were the type T is not restricted by what the function calling it might implement it as, so it threw me off and I didn't think of it, but it does make perfect sense.
Hmm... After looking a bit further into this, It's probably a misconception that I've held and only just now realized wasn't the case. So you were definitely right in it sounding wrong!
My thought was that it was just any parameter that satisfies the constraint, but not necessarily the same. I can see how that would pose certain types of problems and splitting them into two separate type parameters is definitely just something that I need to get used to in certain situations.