元組

{ a, b, c } = { "Hello", "World", "!" }    

IO.puts a # Hello
IO.puts b # World
IO.puts c # !

# Tuples of different size won't match:

{ a, b, c } = { "Hello", "World" } # (MatchError) no match of right hand side value: { "Hello", "World" }

閱讀檔案

模式匹配對於返回元組的檔案讀取操作很有用。

如果檔案 sample.txt 包含 This is a sample text,那麼:

{ :ok, file } = File.read("sample.txt")
# => {:ok, "This is a sample text"}

file
# => "This is a sample text"

否則,如果該檔案不存在:

{ :ok, file } = File.read("sample.txt")
# => ** (MatchError) no match of right hand side value: {:error, :enoent}

{ :error, msg } = File.read("sample.txt")
# => {:error, :enoent}