I want to know the difference between a slice ([T])
and a reference to a slice (&[T])
. I do not get how slices
are unsized types, can't the compiler deduce the size of a slice through the source code?
I understand we need a reference to a slice to make it useful however, this would only make sense If I understand how slices
are unsized.
Moreover, I wanted to know the difference between str
and &str
. I know one is a slice and the other is a reference to a slice and that &str
stores a reference to the start of the string and the length as well. But it confuses me as to why we can't use str
alone without the reference. Why can't we store the size within the str
slice itself. Why do we need a reference to it. I come from a c++ background so maybe using c++ types and terminologies can clear things up!