2

If I have an function that requires a slice of type &[usize] but I have a slice of type &[u8], how do I cast from one type to the other? It seems like it should be easy to cast u8 to usize but I'm not finding a simple solution.

Something like:

use rand::{SeedableRng, StdRng};

// seed = &[1, 2, 3] with type u8
let rng: StdRng = SeedableRng::from_seed(seed);
Rob
  • 3,333
  • 5
  • 28
  • 71
  • if you mean easy as "zero cost" you are very wrong ;) `u8` is not the same size than ... `usize` so I would not call that "easy" but more importantly, your question is suspect I believe your are doing something wrong. Can you include the context that lead you to ask this question ? I believe this would be more efficient to solve your real problem instead of give you a correct technical answer but you will probably use it wrongly. – Stargateur May 04 '18 at 17:32
  • I believe this question is already answered by [Temporarily transmute \[u8\] to \[u16\]](https://stackoverflow.com/q/33968870/155423). If you disagree, please [edit] your question to explain how it's different. Otherwise, we can mark this as already answered. – Shepmaster May 04 '18 at 17:35

0 Answers0