0

Given a tuple: let t = (1, 2, 3);

How can I collect all values of t into, say a Vec<u8>, without explicitly referencing each item (e.g. t.0)?

It seems I cannot iterate or collect them the way I was expecting.

I was expecting something like this to work: let v: Vec<u8> = t.collect();

I want to be able to easily extract all values from any tuple, such that if I have a tuple of n elements (of the same type), I can use the same method to get all of them into an iterable.

loket
  • 131
  • 3
  • 7
  • 2
    You cannot, as tuples can have different element types. Use an array instead. – Chayim Friedman Dec 06 '22 at 08:01
  • There's probably a crate out there which allows iterating homogenous tuples though. Just as there are crates which allow collecting *into* tuples (e.g. [`Itertools::collect_tuple`](https://docs.rs/itertools/latest/itertools/trait.Itertools.html#method.collect_tuple). – Masklinn Dec 06 '22 at 09:10

0 Answers0