Pattern Matching
match
ํค์๋๋ C/C++์ switch
์ ์ ์ฌํ๊ฒ ํ๋ ์ด์์ ํจํด๊ณผ ์ผ์น ์ํฌ ์ ์์ต๋๋ค.
๋น๊ต๋์์ ์์์ ์๋๋ก ์งํ๋๋ฉฐ ์ฒ์ ์ผ์นํ๋ ํจํด์ด ์คํ๋ฉ๋๋ค.
The
match
keyword let you match a value against one or more patterns. The comparisons are done from top to bottom and the first match wins.The patterns can be simple values, similarly to
switch
in C and C++:
fn main() { let input = 'x'; match input { 'q' => println!("Quitting"), 'a' | 's' | 'w' | 'd' => println!("Moving around"), '0'..='9' => println!("Number input"), _ => println!("Something else"), } }
_
ํจํด์ ์ด๋ค ํจํด๊ณผ๋ ๋งค์นญ๋๋ ์์ผ๋ ์นด๋์
๋๋ค.(default)
The
_
pattern is a wildcard pattern which matches any value.