06.分支结构ifelse、match、iflet、matches、Option
<h1>06.分支结构 ifelse、match、iflet</h1>
<h2>1、if-else</h2>
<pre><code>fn main() {
let b = 32u32;
if b == 32 {
println!(&quot;等于32&quot;);
}else if b &lt; 32 {
println!(&quot;小于32&quot;);
}else {
println!(&quot;大于32&quot;);
}
}</code></pre>
<h2>2、match模式匹配</h2>
<pre><code>1、模式匹配
enum Message{
Msg1,
Msg2,
Msg3,
}
fn main() {
let msg = Message::Msg3;
let res = match msg {
Message::Msg1 =&gt; {1},
Message::Msg2 =&gt; {2},
Message::Msg3 =&gt; {1},
_ =&gt; {0}, // default
};
println!(&quot;{}&quot;,res);
}
2、模式匹配解构
enum Action{
Action1(String),
Action2(u32,u32),
Action3(bool,bool,bool),
}
fn main() {
let action = Action::Action3(true,false,true);
let res:String = match action {
Action::Action1(p) =&gt; { &quot;Action1&quot;.to_string() }
Action::Action2(p1, p2) =&gt; {&quot;Action2&quot;.to_string()}
Action::Action3(p1, p2, p3) =&gt; {&quot;Action3&quot;.to_string()}
};
println!(&quot;{}&quot;,res);
}
3、模式匹配穷尽
如果没有全部覆盖,会报错
error[E0004]: non-exhaustive patterns: `Message::Msg3()` not covered
enum Message{
Msg1,
Msg2,
Msg3,
}
fn main() {
let msg = Message::Msg3;
let res = match msg {
Message::Msg1 =&gt; {1},
Message::Msg2 =&gt; {2},
};
println!(&quot;{}&quot;,res);
}
3、模式匹配-通配符_或other
当我们不想在匹配时列出所有值的时候,可以使用_通配符/other,匹配剩余所有情况
enum Message{
Msg1,
Msg2,
Msg3,
}
fn main() {
let msg = Message::Msg3;
let res = match msg {
Message::Msg1 =&gt; {1},
Message::Msg2 =&gt; {2},
Message::Msg3 =&gt; {1},
_ =&gt; {0}, // default
other =&gt; {0}, // default
};
println!(&quot;{}&quot;,res);
}
4、模式匹配-..忽略部分值
// 忽略部分元素
struct Point {
x: i32,
y: i32,
z: i32,
}
let origin = Point { x: 0, y: 0, z: 0 };
match origin {
Point { x, .. } =&gt; println!(&quot;x is {}&quot;, x),
}
// 忽略元组部分元素
fn main() {
let numbers = (2, 4, 8, 16, 32);
match numbers {
(first, .., last) =&gt; {
println!(&quot;Some numbers: {}, {}&quot;, first, last);
},
}
}
// Rust 无法判断,second 应该匹配 numbers 中的第几个元素,会报错
fn main() {
let numbers = (2, 4, 8, 16, 32);
match numbers {
(.., second, ..) =&gt; {
println!(&quot;Some numbers: {}&quot;, second)
},
}
}
5、进一步判断
//if x &lt; 5是第二个判断条件
let num = Some(4);
match num {
Some(x) if x &lt; 5 =&gt; println!(&quot;less than five: {}&quot;, x),
Some(x) =&gt; println!(&quot;{}&quot;, x),
None =&gt; (),
}
6、@绑定与解构
// 相当于别名
#[derive(Debug)]
struct Point {
x: i32,
y: i32,
}
fn main() {
// 绑定新变量 `p`,同时对 `Point` 进行解构
let p @ Point {x: px, y: py } = Point {x: 10, y: 23};
println!(&quot;x: {}, y: {}&quot;, px, py);
println!(&quot;{:?}&quot;, p);
let point = Point {x: 10, y: 5};
if let p @ Point {x: 10, y} = point {
println!(&quot;x is 10 and y is {} in {:?}&quot;, y, p);
} else {
println!(&quot;x was not 10 :(&quot;);
}
}</code></pre>
<h2>3、iflet匹配</h2>
<pre><code>1、模式匹配
fn main() {
let v = Some(3u8);
match v {
Some(3) =&gt; {println!(&quot;3&quot;)}
_ =&gt; {println!(&quot;NaN&quot;)}
}
}
2、iflet匹配
相当于只有一种匹配,且解构,if a(3) = b(3) {...}
fn main() {
let v = Some(3u8);
if let Some(3) = v {
println!(&quot;three&quot;);
}
}
</code></pre>
<h2>4、matches!宏</h2>
<pre><code>fn main() {
// 例子1
let foo = &#039;f&#039;;
// match写法
let res1 = match foo {
&#039;A&#039;..=&#039;Z&#039; | &#039;a&#039;..=&#039;z&#039; =&gt; true,
_ =&gt; false,
};
println!(&quot;{}&quot;, res1);
// matches!宏写法
println!(&quot;{}&quot;,matches!(foo, &#039;A&#039;..=&#039;Z&#039; | &#039;a&#039;..=&#039;z&#039;));
// 例子2
let bar = Some(4);
let res2 = match bar {
Some(4) =&gt; true,
_ =&gt; false,
};
println!(&quot;{}&quot;, res2);
println!(&quot;{}&quot;,matches!(bar, Some(x) if x &gt; 2));
}</code></pre>
<h2>5、可选类型Option<T></h2>
<pre><code>fn main() {
fn plus_one(x: Option&lt;i32&gt;) -&gt; Option&lt;i32&gt; {
match x {
None =&gt; None,
Some(i) =&gt; Some(i + 1),
}
}
let five = Some(5);
let six = plus_one(five);
//match解构
let ss = match six {
None =&gt; {0}
Some(p) =&gt; {p}
};
println!(&quot;{}&quot;, ss);
// iflet解构
if let Some (i) = six{
println!(&quot;{}&quot;, i)
}
}</code></pre>