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

36 lines
1.0 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

---
title: Tuples
localeTitle: 元组
---
## 元组
在Elixir中元组是一种数据结构可以保存任何值或混合类型。元组由花括号定义它们的索引从0开始。因为元组在内存中连续存储所以从它们获取数据是一个非常快速的操作。
```elixir
iex> tuple = {:atom, "string"}
{:atom, "string"}
iex> elem(tuple, 0)
:atom
```
## 不变性
Elixir中的元组是不可变的因此进行修改将返回一个全新的元组 - 将原始内容保存在内存中。
```elixir
iex> tuple = {:atom, "string"}
{:atom, "string"}
iex> put_elem(tuple, 1, true)
{:atom, true}
iex> tuple
{:atom, "string"}
```
## 模式匹配
Elixir中元组的最常见用法是作为函数的返回。例如 `{:ok, "Hello World\n"}` 这是非常有用的,因为它允许使用模式匹配来处理这些返回。
#### 更多信息:
* [elixir-lang.org |递归](https://elixir-lang.org/getting-started/basic-types.html#tuples)
* [hexdocs |枚举](https://hexdocs.pm/elixir/Tuple.html)