Rust 编程视频教程(进阶)——024_1 所有模式的语法 1
视频地址
头条地址:https://www.ixigua.com/i677586170644791348...
B站地址:https://www.bilibili.com/video/av81202308/
源码地址
github地址:https://github.com/anonymousGiga/learn_rus...
讲解内容
1、匹配字面值
例子:
let x = 1;
match x {
1 => println!("one"),
2 => println!("two"),
3 => println!("three"),
_ => println!("anything"),
}
2、匹配命名变量
例子:
fn main() {
let x = Some(5);
let y = 10; //1处
match x {
Some(50) => println!("Got 50"),
Some(y) => println!("Matched, y = {:?}", y), //此处的y和上面1处的y不一样,此处是引入的变量y覆盖之前的y
_ => println!("Default case, x = {:?}", x),
}
println!("at the end: x = {:?}, y = {:?}", x, y);
}
3、多个模式
例子:
let x = 1;
match x {
1 | 2 => println!("one or two"),
3 => println!("three"),
_ => println!("anything"),
}
说明: |表示或,即匹配1或者2
本作品采用《CC 协议》,转载必须注明作者和本文链接