<예제 코드_1>
fn main() {
let x = Some(5);
let y = 10;
match x {
Some(50) => println!("Got 50"),
Some(y) => println!("Matched, y = {:?}", y),
_ => println!("Default case, x = {:?}", x),
}
println!("at the end: x = {:?}, y = {:?}", x, y);
}
<예제 코드_2>
fn main() {
let x = 1;
match x {
1 | 2 => println!("one or two"),
3 => println!("three"),
_ => println!("anything"),
}
}
<예제 코드_3>
fn main() {
let x = 5;
match x {
1..=5 => println!("one through five"),
_ => println!("something else"),
}
}
<예제 코드_4>
fn main() {
let x = 'c';
match x {
'a'..='j' => println!("Early ASCII letter"),
'k'..='z' => println!("Later ASCII letter"),
_ => println!("Something else"),
}
}
<예제 코드_5>
struct Point {
x: i32,
y: i32,
}
fn main() {
let p = Point { x: 0, y: 7 };
// let Point { x: a, y: b } = p;
// assert_eq!(0, a);
// assert_eq!(6, b);
// assert_eq!(7, b);
let Point { x, y } = p;
assert_eq!(0, x);
assert_eq!(7, y);
}
<예제 코드_6>
struct Point {
x: i32,
y: i32,
}
fn main() {
let p = Point { x: 0, y: 7 };
match p {
Point { x, y: 0 } => println!("On the x axis at {}", x),
Point { x: 0, y } => println!("On the y axis at {}", y),
Point { x, y } => println!("On neither axis: ({}, {})", x, y),
}
}
<예제 코드_7>
enum Message {
Quit,
Move { x: i32, y: i32 },
Write(String),
ChangeColor(i32, i32, i32),
}
fn main() {
let msg = Message::ChangeColor(0, 160, 255);
match msg {
Message::Quit => {
println!("the Quit variant has not data to destruction.");
},
Message::Move { x, y } => {
println!(
"Move in the x direction {} and in the y direction {}",
x,
y
);
},
Message::Write(text) => {
println!("Text message: {}", text)
},
Message::ChangeColor(r, g ,b) => {
println!(
"Change the color to red {}, green {}, and blue {}",
r,
g,
b
);
}
}
}
<예제 코드_8>
struct Point {
x: i32,
y: i32,
}
fn main() {
let points = vec!{
Point { x: 0, y: 0 },
Point { x: 1, y: 5 },
Point { x: 10, y: -3 },
};
let sum_of_squares: i32 = points
.iter()
.map(|&Point { x, y }| x * x + y * y)
.sum();
}
<예제 코드_9>
fn foo(_: i32, y: i32) {
println!("This code only uses the y parameter: {}", y);
}
fn main() {
foo(3, 4);
}
<예제 코드_10>
fn main() {
let mut setting_value = Some(5);
let new_setting_value = Some(10);
match (setting_value, new_setting_value) {
(Some(_), Some(_)) => {
println!("Can't overwrite an existing customized value");
}
_ => {
setting_value = new_setting_value;
}
}
println!("Setting is {:?}", setting_value);
}
<에제 코드_11>
fn main() {
let numbers = (2, 4, 8, 16, 32);
match numbers {
(first, _, third, _, fifth) => {
println!("Some numbers: {}, {}, {}", first, third, fifth)
},
}
}
<예제 코드_12>
fn main() {
let s = Some(String::from("Hello!"));
if let Some(_) = s {
println!("found a string");
}
println!("{:?}", s);
}
<예제 코드_13>
struct Point {
x: i32,
y: i32,
z: i32,
}
fn main() {
let origin = Point { x: 0, y: 0, z: 0 };
match origin {
Point { x, .. } => println!("x is {}", x),
}
let numbers = (2, 4, 8, 16, 32);
match numbers {
// .. 은 나머지를 의미
(first, .., last) => {
println!("Some numbers: {}, {}", first, last);
},
}
match numbers {
// .. 은 tuple 당 한번만 쓸 수 있음
// 온전한 실행을 위해선 아무 ..을 하나 지우면 됨
(.., second, ..) => {
println!("Some numbers: {}", second)
},
}
}
<예제 코드_14>
fn main() {
let mut robot_name = Some(String::from("Bors"));
match robot_name {
Some(ref mut name) => *name = String::from("Another name"),
None => (),
}
println!("robot_name is: {:?}", robot_name);
}
<예제 코드_15>
fn main() {
let num = Some(4);
match num {
Some(x) if x < 5 => println!("less that five: {}", x),
Some(x) => println!("{}", x),
None => (),
}
}
<예제 코드_16>
fn main() {
let x = Some(5);
let y = 10;
match x {
Some(50) => println!("Got 50"),
Some(n) if n == y => println!("Matched, n = {:?}", n),
_ => println!("Default case, x = {:?}", x),
}
println!("at the end: x = {:?}, y = {:?}", x, y);
}
<예제 코드_17>
fn main() {
let x = 4;
let y = false;
match x {
4 | 5 | 6 if y => println!("yes"),
_ => println!("no"),
}
}
<예제 코드_18>
enum Message {
Hello { id: i32 },
}
fn main() {
let msg = Message::Hello { id: 5 };
match msg {
Message::Hello { id: id_variable @ 3..=7 } => {
println!("Found an id in range: {}", id_variable);
},
Message::Hello { id: 10..=12 } => {
println!("Found an id in another range");
},
Message::Hello { id } => {
println!("Found some other id: {}", id);
}
}
}
<참조 1> https://rinthel.github.io/rust-lang-book-ko/ch18-03-pattern-syntax.html
<참조 2>
'Rust' 카테고리의 다른 글
[Rust] Thunk 예제 (47일차) (0) | 2023.01.29 |
---|---|
[Rust] 고급 Trait 예제 (46일차) (0) | 2023.01.29 |
[Rust] Trait 예제 (44일차) (0) | 2023.01.27 |
[Rust] 공유 상태 동시성 예제 (Shared State Concurrency, 43일차) (0) | 2023.01.26 |
댓글