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.