Skip to content

Commit 3275f51

Browse files
committed
add if_expressions.rs example file
1 parent 0431a2c commit 3275f51

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed

examples/if_expressions.rs

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
fn f(n: i32) {
2+
// ()-typed
3+
if n == 2 {
4+
}
5+
if n == 1 {
6+
} else if n == 2 {
7+
return;
8+
} else {
9+
}
10+
// ()-typed (if let)
11+
if let 1 = n {
12+
} else if let 2 = n {
13+
return;
14+
} else if n == 2 {
15+
return;
16+
} else {
17+
}
18+
// simple if/else expression
19+
let y = if x == 5 { 5 } else { 10 };
20+
let z = Some(5);
21+
// if expression with an else if branch
22+
let y = if z.is_some() {
23+
30
24+
} else if z.is_none() {
25+
99
26+
} else {
27+
0
28+
};
29+
// if expression with an else if let branch
30+
let y = if z.is_some() {
31+
30
32+
} else if let None = y {
33+
99
34+
} else {
35+
0
36+
};
37+
// if let expression with both kinds of else if
38+
let y = if let Some(3) = y {
39+
30
40+
} else if let Some(4) = y {
41+
} else if y == Some(10) {
42+
100
43+
} else {
44+
0
45+
};
46+
// if and else-if with let-else chain
47+
if let Some(3) = y && let None = z {
48+
} else if let None = y && let Some(_) = z {
49+
}
50+
}

0 commit comments

Comments
 (0)