leisurely8rust


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

页面列表

ITEM_HTML