Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I convert a reference to any type to a reference to an array of length 1?

Tags:

rust

I have an instance of type &T. How do I cast it to &[T; 1]? Something like std::slice::from_ref, except that it should return an array, instead of a slice.

like image 776
alagris Avatar asked Jul 28 '21 16:07

alagris


Video Answer


1 Answers

Use array::from_ref or array::from_mut.

fn example(a: &String) -> &[String; 1] {
    std::array::from_ref(a)
}

See also:

  • How to create a slice from a single element without copying that element?
like image 147
Shepmaster Avatar answered Sep 22 '22 11:09

Shepmaster