4.6 结构、元祖和穷举 (Structs, Tuples, and Enums)
结构 (Structs)
Structs in Sway are a named grouping of types. You may also be familiar with structs via another name: product types. Sway does not make any significantly unique usages of structs; they are similar to most other languages which have structs. If you're coming from an object-oriented background, a struct is like the data attributes of an object.
Sway中的结构是一种预先命名的类型分组。你可能也熟悉结构的另一个名字:产品类型。Sway并没有对结构进行任何明显的独特使用;它们与其他大多数拥有结构的语言相似。如果你有面向对象的背景,结构就像是对象的数据属性。
Firstly, we declare a struct named Foo
with two fields. The first field is named bar
and it accepts values of type u64
, the second field is named baz
and it accepts bool
values.
首先,我们声明一个名为 Foo
的结构,有两个字段。第一个字段名为 bar
,它接受 u64
类型的值,第二个字段名为 baz
,它接受 bool
值。
In order to instantiate the struct we use struct instantiation syntax, which is very similar to the declaration syntax except with expressions in place of types.
为了实例化结构,我们使用 结构实例化 struct instantiation syntax 语法,它与声明语法非常相似,只是用表达式代替了类型。
There are three ways to instantiate the struct.
有三种方法来实例化结构。
Hardcoding values for the fields 硬编码字段的值
Passing in variables with names different than the struct fields 传入名称与结构字段不同的变量
Using a shorthand notation via variables that are the same as the field names 对与字段名相同的变量使用速记符号
Note You can mix and match all 3 ways to instantiate the struct at the same time. Moreover, the order of the fields does not matter when instantiating however we encourage declaring the fields in alphabetical order and instantiating them in the same alphabetical order 注意 您可以同时混合使用这三种方式来对结构进行实例化。此外,实例化时字段的顺序并不重要,但我们鼓励按字母顺序对字段进行声明,并按相同的字母顺序进行实例化。
Furthermore, multiple variables can be extracted from a struct using the destructuring syntax.
此外,可以使用解构语法从一个结构中提取多个变量。
结构内存布局 (Struct Memory Layout)
Note This information is not vital if you are new to the language, or programming in general 注意 如果你是这个语言的新手,或者是一般的编程新手,这个信息并不重要。
Structs have zero memory overhead. What that means is that in memory, each struct field is laid out sequentially. No metadata regarding the struct's name or other properties is preserved at runtime. In other words, structs are compile-time constructs. This is the same in Rust, but different in other languages with runtimes like Java.
结构的内存开销为零。这意味着,在内存中,每个结构字段都是按顺序排列的。在运行时,没有关于结构名称或其他属性的元数据被保留。换句话说,结构是编译时的构造的。这在Rust中是一样的,但在其他有运行时的语言中则不同,比如Java。
元祖 (Tuples)
Tuples are a basic static-length type which contain multiple different types within themselves. The type of a tuple is defined by the types of the values within it, and a tuple can contain basic types as well as structs and enums.
元组是一种基本静态长度类型,其中包含多个不同类型。元组的类型由其中的值的类型定义,元组可以包含基本类型以及结构和穷举。
You can access values directly by using the .
syntax. Moreover, multiple variables can be extracted from a tuple using the destructuring syntax.
你可以通过使用 .
语法直接访问数值。此外,可以使用解构语法从一个元组中提取多个变量。
穷举 (Enums)
Enumerations, or enums, are also known as sum types. An enum is a type that could be one of several variants. To declare an enum, you enumerate all potential variants.
穷举,或称 enums
,也被称为 sum类型
。穷举是一个可以成为几个变体之一的类型。为了声明一个穷举,你要穷举所有潜在的变体。
Here, we have defined five potential colors. Each enum variant is just the color name. As there is no extra data associated with each variant, we say that each variant is of type ()
, or unit.
在这里,我们定义了五个潜在的颜色。每个穷举变体只是颜色名称。由于每个变体没有多余的数据,我们说每个变体都是()
类型的,或者说是单位。
结构的穷举 (Enums of Structs)
It is also possible to have an enum variant contain extra data. Take a look at this more substantial example, which combines struct declarations with enum variants:
让穷举变体包含额外的数据也是可能的。请看这个更实质性的例子,它结合了结构声明和穷举变体:
穷举的穷举 (Enums of Enums)
It is possible to define enums of enums:
我们也可以定义穷举的穷举:
首选的用法 (Preferred usage)
The preferred way to use enums is to use the individual (not nested) enums directly because they are easy to follow and the lines are short:
使用穷举的首选方法是直接使用独立的(非嵌套的)穷举,因为它们很容易理解,而且行数很短:
不推荐 (Inadvisable)
If you wish to use the nested form of enums via the Error
enum from the example above, then you can instantiate them into variables using the following syntax:
如果你想通过上面例子中的Error
穷举使用嵌套形式的穷举,那么你可以使用以下语法将它们实例化为变量:
Key points to note: 需要注意的关键点:
You must import all of the enums you need instead of just the
Error
enum 你必须导入所有你需要的穷举,而不是只导入Error
穷举The lines may get unnecessarily long (depending on the names) 行数可能会变得不必要的长(取决于名称)
The syntax is not the most ergonomic 语法不是最符合人体工程学的
穷举内存布局 (Enum Memory Layout)
Note This information is not vital if you are new to the language, or programming in general. 注意 如果你是该语言的新手,或者是一般的编程新手,这些信息并不重要。
Enums do have some memory overhead. To know which variant is being represented, Sway stores a one-word (8-byte) tag for the enum variant. The space reserved after the tag is equivalent to the size of the largest enum variant. So, to calculate the size of an enum in memory, add 8 bytes to the size of the largest variant. For example, in the case of Color
above, where the variants are all ()
, the size would be 8 bytes since the size of the largest variant is 0 bytes.
穷举确实有一些内存开销。为了知道哪个变量正在被表示,Sway为穷举变量存储了一个单字(8字节)的标签。标签后,保留的空间相当于最大的穷举变量的大小。因此,要计算一个穷举在内存中的大小,需要在最大的变体的大小上加上8字节。例如,在上面 Color
的例子中,变体都是()
,大小将是8字节,因为最大变体的大小是0字节。
Last updated