This actually does not fail. I think I mixed something up. I'm sorry. I had a previous version that looked different when it was failing. Maybe I'll figure out again what confused me.
Okay, the surprising thing is this:
fn main() {
let array: [u8; 1] = [0u8; 1];
let array_ref: &[u8; 1] = &[0u8; 1];
let slice: &[u8] = &vec![0u8];
assert!(array == slice); // works
assert!(array == array_ref); // fails
}
Errors:
Compiling playground v0.0.1 (/playground)
error[E0277]: can't compare `[u8; 1]` with `&[u8; 1]`
--> src/main.rs:6:19
|
6 | assert!(array == array_ref); // fails
| ^^ no implementation for `[u8; 1] == &[u8; 1]`
|
= help: the trait `PartialEq<&[u8; 1]>` is not implemented for `[u8; 1]`
= help: the following other types implement trait `PartialEq<Rhs>`:
<&[B] as PartialEq<[A; N]>>
<&[T] as PartialEq<Vec<U, A>>>
<&mut [B] as PartialEq<[A; N]>>
<&mut [T] as PartialEq<Vec<U, A>>>
<[A; N] as PartialEq<&[B]>>
<[A; N] as PartialEq<&mut [B]>>
<[A; N] as PartialEq<[B; N]>>
<[A; N] as PartialEq<[B]>>
and 3 others
For more information about this error, try `rustc --explain E0277`.
error: could not compile `playground` due to previous error
I would expect that if I can't compare [0; 1]
with &[0; 1]
, then I would also not be able to compare [0; 1]
with a slice. But comparison with &[u8]
works (but not with &[u8; 1]
).
That is a bit... strange (but may be just like how the std
library works).
It works exactly because of this impl
:
So unsized coercion is indeed not involved.
Side question: How can it be existing since Rust 1.0 while it uses const generics? (Might need to look that up in the repository history.)