freeCodeCamp/guide/chinese/elixir/tuples/index.md

1.0 KiB
Raw Blame History

title localeTitle
Tuples 元组

元组

在Elixir中元组是一种数据结构可以保存任何值或混合类型。元组由花括号定义它们的索引从0开始。因为元组在内存中连续存储所以从它们获取数据是一个非常快速的操作。

iex> tuple = {:atom, "string"} 
 {:atom, "string"} 
 iex> elem(tuple, 0) 
 :atom 

不变性

Elixir中的元组是不可变的因此进行修改将返回一个全新的元组 - 将原始内容保存在内存中。

iex> tuple = {:atom, "string"} 
 {:atom, "string"} 
 iex> put_elem(tuple, 1, true) 
 {:atom, true} 
 iex> tuple 
 {:atom, "string"} 

模式匹配

Elixir中元组的最常见用法是作为函数的返回。例如 {:ok, "Hello World\n"} 这是非常有用的,因为它允许使用模式匹配来处理这些返回。

更多信息: