basic/match-pattern/match-if-let #745
Replies: 45 comments 68 replies
-
|
Beta Was this translation helpful? Give feedback.
-
if let Some(3) = v { |
Beta Was this translation helpful? Give feedback.
-
后面的 变量覆盖,其实就是 Shadowing 吧? |
Beta Was this translation helpful? Give feedback.
-
if let Some(age) = age { |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
matches! 没有介绍太明白,这种表达式 Some(x) if x > 2 从哪来的就很突兀,参考: |
Beta Was this translation helpful? Give feedback.
-
fn main() { 同时通过解构Some(age)=Some(30), 即age=30 |
Beta Was this translation helpful? Give feedback.
-
如果使用if let 不解构, 那干嘛不使用 if expression {} |
Beta Was this translation helpful? Give feedback.
-
这里看到一个关于 if let 语法的讲解,觉得蛮好的,分享给大家:https://www.jianshu.com/p/3004a981750f |
Beta Was this translation helpful? Give feedback.
-
本章的变量覆盖应该和前面变量章节的变量遮蔽是一个东西 |
Beta Was this translation helpful? Give feedback.
-
在第一个例子中, |
Beta Was this translation helpful? Give feedback.
-
match dire {
East => println!("East"),
Direction::North | Direction::South => {
println!("South or North");
},
_ => println!("West"),
}; 第一个,可以直接是East,后面Direction::North | Direction::South 为什么不能省去Direction:: ? |
Beta Was this translation helpful? Give feedback.
-
fn main() {
let age = Some(30);
println!("在匹配前,age是{:?}",age);
if let Some(age) = age {
println!("匹配出来的age是{}",age);
}
println!("在匹配后,age是{:?}",age);
} 这里的 基于学习的内容,似乎模式匹配有两种功能:
例子当中的用法,似乎属于前者,即字面意义的模式匹配,并非匹配具体的值。因此,age的模式就是 类似地,for循环的变量用法也是: for v in Vec1 {
println!("{}", v);
} v就是临时变量,不管它叫什么名字,都可以进行遍历。 |
Beta Was this translation helpful? Give feedback.
-
感觉跟 kotlin 的 when 类似 val url = when (it.type) {
1 -> "/my/banned/list/details"
2 -> "/my/black/list/details"
else -> "/my/violations/help/details"
} |
Beta Was this translation helpful? Give feedback.
-
Done。 意思是说,我所要进行匹配的是是age_i,这个是i32类型的,编译器期望“=”左边匹配项应该也是一个{interage类型},通过这个错误的例子,再多想一想,如果把age_i换成age的话,那就过编译器这一关了,运行的时候在匹配上后,这里还有一次模式匹配,可以理解为这里来了一个新的age变量覆盖了原来的age,作用域在if let表达式中,这个age的类型是i32类型,出了iflet表达式之后,这个新的age出了作用域结束了,再后面使用的age还是Some(30)。 |
Beta Was this translation helpful? Give feedback.
-
但是,实际上这行代码会报错,因为你无法将 x 直接跟一个枚举成员进行比较。好在,你可以使用 match 来完成,但是会导致代码更为啰嗦,是否有更简洁的方式?答案是使用 matches!: fn main() { |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
基本上就是弱化版的 python match-case,当然,不能跟动态语言比灵活性,而且 python 也没有这种编译器检查。 |
Beta Was this translation helpful? Give feedback.
-
不喜欢if let这种语法糖,很恶心人,不够简明,应该向python学习才是。Mark 23.8.29 |
Beta Was this translation helpful? Give feedback.
-
if let 结合这个例子看 #![allow(unused)]
fn main() {
#[derive(Debug)]
enum PokerCard {
Clubs(u8),
Spades(u8),
Diamonds(u8),
Hearts(u8),
}
let card = PokerCard::Clubs(123);
if let PokerCard::Clubs(123) = card {
println!("1. 精确匹配enum类型和附加数据也相等");
println!("card变量是{:?}", card);
}
if let PokerCard::Clubs(number) = card {
println!("2. enum类型,附加数据放到变量中");
println!("card变量是{:?}", card);
println!("number是{:?}", number);
}
if let PokerCard::Clubs(card) = card {
println!("3. enum类型,附加数据放到同名变量中,被遮蔽");
println!("card变量是{:?}", card);
println!("卡片number是{:?}", card);
}
println!("card变量是{:?}", card);
}
// 结果
// 1. 精确匹配enum类型和附加数据也相等
// card变量是Clubs(123)
// 2. enum类型,附加数据放到变量中
// card变量是Clubs(123)
// number是123
// 3. enum类型,附加数据放到同名变量中,被遮蔽
// card变量是123
// 卡片number是123
// card变量是Clubs(123) |
Beta Was this translation helpful? Give feedback.
-
请问为什么x 不可以直接跟一个枚举成员进行比较呢,底层是啥原因呢 |
Beta Was this translation helpful? Give feedback.
-
在标题 match匹配 部分最后一段
有个疑问,这段最后一句“同时最后一行代码需要是一个表达式”是否正确?因为这段话上面的代码 enum Direction {
East,
West,
North,
South,
}
fn main() {
let dire = Direction::South;
match dire {
Direction::East => println!("East"),
Direction::North | Direction::South => {
println!("South or North");
},
_ => println!("West"),
};
} 这里的 |
Beta Was this translation helpful? Give feedback.
-
ChangeColorRGB(u16, u16, u16) 形参定义成 ChangeColorRGB(u8, u8, u8) 更准确点 |
Beta Was this translation helpful? Give feedback.
-
调试的时候发现 |
Beta Was this translation helpful? Give feedback.
-
不少东西给出的太突兀,没有等价的解释说明。 |
Beta Was this translation helpful? Give feedback.
-
不太明白 |
Beta Was this translation helpful? Give feedback.
-
总感觉
|
Beta Was this translation helpful? Give feedback.
-
关于变量遮蔽,官方英文书上的例子更好理解:
变量number 如果复合Some(i)的模式,number就会被赋值给临时变量 |
Beta Was this translation helpful? Give feedback.
-
#[derive(Debug, PartialEq)] fn main() { |
Beta Was this translation helpful? Give feedback.
-
let thread: thread::JoinHandle<()> = thread::spawn(move || loop {
let msg: Result<Job, mpsc::RecvError> =
receiver.lock().unwrap().recv();
match msg {
Ok(job) => {
println!("Worker {id} got a job; executing.");
job();
}
Err(_) => {
println!("Worker {id} disconnected; shutting down.");
break;
}
}
}); let response: Frame = match Command::from_frame(frame).unwrap() {
Set(cmd) => {
db.insert(cmd.key().to_string(), cmd.value().to_vec());
Frame::Simple("OK".to_string())
}
Get(cmd) => {
if let Some(value) = db.get(cmd.key()) {
Frame::Bulk(value.clone().into())
} else {
Frame::Null
}
}
cmd => panic!("unimplemented {:?}", cmd),
}; 在上述的两个例子代码中,match的分支到底是看作代码块还是函数?如果是看作函数但是它可以直接break中断循环,如果是看作代码块但是它又像函数一样返回值到变量,而且很难受的是match里面的返回不能用return |
Beta Was this translation helpful? Give feedback.
-
basic/match-pattern/match-if-let
https://course.rs/basic/match-pattern/match-if-let.html
Beta Was this translation helpful? Give feedback.
All reactions