0

I found numerous answers related to converting vectors into arrays, but all deal with simple types inside the vector, which in my case doesn't work.

I have a Vec<(String, PayloadType)> where PayloadType is a rich enum.

I need to convert it into an array [(String, PayloadType), _].

Some attempts:

try_into():

492 |         Payload::from(v[..].try_into().unwrap())
    |                             ^^^^^^^^ the trait `TryFrom<&[(String, mpl_token_auth_rules::payload::PayloadType)]>` is not implemented for `[(String, mpl_token_auth_rules::payload::PayloadType); _]`

into_inner():

492 |         Payload::from(v.into_inner().unwrap())
    |                         ^^^^^^^^^^ method not found in `Vec<(String, mpl_token_auth_rules::payload::PayloadType)>`

What's the right way to do it?

PS, the broader goal is to convert a hashmap like this:

pub struct Payload {
    map: HashMap<String, PayloadType>,
}

into an array like this:

[(String, PayloadType); N]

I went about it by converting it to Vec first, but open to a better way.

ilmoi
  • 1,994
  • 2
  • 21
  • 45
  • 2
    Do you mean an *array* (i.e. an owned, statically-sized collection of data), or a *slice* (that is, a borrowed collection of contiguous data of potentially dynamic size)? If it's the former, you can't, because you don't statically know the size of the array. If it's the latter, `&v[..]` gets you all the way there. And consider whether a vector works for your use case to begin with. `Vec` is literally designed to be "array of dynamic size" and supports many of the same operations. – Silvio Mayolo Jan 24 '23 at 00:20
  • Just remove `[..]` from your first attempt, you can `try_into` vecs into arrays, not references to slices into arrays. See this [playground](https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=6e8781ab8a0ab9d1c149682186bf8eef) – cafce25 Jan 24 '23 at 00:20
  • @Sil yes you can `try_into` a `Vec` into an array, it will return the original `Vec` if it doesn't have the correct number of items. – cafce25 Jan 24 '23 at 00:28
  • you hate vector of what ? – Stargateur Jan 24 '23 at 04:34

0 Answers0