Trait specialization for slice


I try to use the specialization feature of the nightly and I have something I can explains why it's not working. Can somebody tell me why the last call of the following code do not take the specialization?
without the specialization I have the expected behavior:

Thank you!

Hi, it's because [1, 2, 3] is of type [i32; 3] and not [i32].
You can see it using this for example:

let _: () = [1, 2, 3];

If you want to use the specialization you can do:

[1, 2, 3].as_ref().something();
1 Like

impl<T> Do for T { ... }

Generic type parameters are implicitly constrained by Sized by default. But slice types are not Sized and not matching to the impl. You have to specify T: ?Sized to out-out Sized constraint.

1 Like

Thank you both,
Is there a way to specialize for [i32;N]? I can't find a way to do to it.

You need const generics, they are nightly only but work is been done to change that. playground

1 Like

Awesome! A big thank you!

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.