match
expressions
๋ง์น if let
๋ฅผ ์ฌ๋ฌ๊ฐ ์ ์ฉํ ๊ฒ๊ณผ ๊ฐ์ด match
ํค์๋๋ ํ๋ ์ด์์ ํจํด์ ์ฐพ๋๋ฐ ์ฌ์ฉ๋ฉ๋๋ค.
The
match
keyword is used to match a value against one or more patterns. In that sense, it works like a series ofif let
expressions:
fn main() { match std::env::args().next().as_deref() { Some("cat") => println!("Will do cat things"), Some("ls") => println!("Will ls some files"), Some("mv") => println!("Let's move some files"), Some("rm") => println!("Uh, dangerous!"), None => println!("Hmm, no program name?"), _ => println!("Unknown program name!"), } }
if let
์ฒ๋ผ ๊ฐ ๋งค์น ์(match arm)์ ๋ง์ง๋ง ํํ์์ด ํ์
์ด ๋๋ฉฐ ๋ชจ๋ ์์ ๋์ผํ ํ์
์ด์ด์ผ ํฉ๋๋ค. ์์ ์์ ์์ ํ์
์ ()
(๋ฐํ๊ฐ ์์)์
๋๋ค.
๋ฌ์คํธ์ ํจํด๋งค์นญ์ ์ฐธ์กฐํ์ธ์
Like if let
, each match arm must have the same type. The type is the last
expression of the block, if any. In the example above, the type is ()
.
See pattern matching for more details on patterns in Rust.
์ญ์ฃผ
- ์๋ฌธ์ด ์ข ๋ํดํ๋ฐ, ์์์์ ๊ฐ ์์ ํ์
์ ๋ฆฌํด ์์(์ถ๋ ฅ๋ง ํ๊ณ ๋)์
๋๋ค(๋ฌ์คํธ์์
()
๋ผ๊ณ ํฉ๋๋ค.ํจ์
์์ค ์ค๊ฐ ์ฃผ์ ์ฐธ์กฐ) - ๊ฐ ์๋ ๋ธ๋ก
{}
์ผ๋ก ํํ๊ฐ๋ฅํ๊ณ ์ด ์ญ์ ํจ์์ฒ๋ผ ๋ง์ง๋ง ํํ์์ผ๋ก ๋ฆฌํด ์ํฌ ์ ์๋๋ฐ ์ด๋ฅผ ๊ฐ ์์ ํ์ ์ผ๋ก ํ๋ค๋ ์๋ฏธ. - ์์ ๋ฅผ ์ฌ๋ฌ ๋ฐํ์์ผ๋ก ํ ์คํธ ํด๋ณด์๊ธฐ ๋ฐ๋๋๋ค.