Warm tip: This article is reproduced from serverfault.com, please click

What is the difference between tuples and array in rust?

发布于 2018-10-20 03:26:18

The only real difference I can figure out after reading the beginner guide, is that in tuple you can have values of multiple types? Both are immutable?

And what are the use cases where I'd want a tuple or array, apart from the obvious one.

Questioner
abhishek_M
Viewed
0
Sven Marnach 2018-10-20 22:12:41

An array is a list of items of homogeneous type. You can iterate over it and index or slice it with dynamic indices. It should be used for homegeneous collections of items that play the same role in the code. In general, you will iterate over an array at least once in your code.

A tuple is a fixed-length agglomeration of heterogeneous items. It should be thought of as a struct with anonymous fields. The fields generally have different meaning in the code, and you can't iterate over it.