Rust 中的元组和数组有什么区别? [英] What is the difference between tuples and array in rust?

查看:50
本文介绍了Rust 中的元组和数组有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在阅读初学者指南后,我能弄清楚的唯一真正区别是,在元组中您可以拥有多种类型的值吗?两者都是不可变的?

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.

推荐答案

数组是同类项的列表.您可以迭代它并使用动态索引对其进行索引或切片.它应该用于在代码中扮演相同角色的项目的原生集合.通常,您将在代码中至少迭代一次数组.

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.

元组是异质项的固定长度聚集.它应该被认为是一个带有匿名字段的 struct.这些字段在代码中通常具有不同的含义,您无法对其进行迭代.

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.

这篇关于Rust 中的元组和数组有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆