basic/match-pattern/match-if-let #745
Replies: 51 comments 75 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.
-
在标题 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.
-
假设有一个枚举类型 enum Num{
Val(i32)
} 那么在match中使用模式匹配的模式绑定 let num = Num::Val(100);
match num {
Num::Val(x) => println!("{}",x)
} 请问这里的Num::Val(x)是否是枚举类型的解构呢? |
Beta Was this translation helpful? Give feedback.
-
#[derive(Debug)] fn main() {
} |
Beta Was this translation helpful? Give feedback.
-
if let Some(3) = v {
|
Beta Was this translation helpful? Give feedback.
-
在Rust 1.80.0中,模式可以使用独占端点,写为a..b或..b,类似于Range和RangeTo表达式类型,望作者大大更新。同时要感谢作者大大和本书编纂者们对Rust社区作出的非凡贡献,使我们收获了一本优秀的Rust入门书,Thanks |
Beta Was this translation helpful? Give feedback.
-
fn main() {
let num = 50;
match num {
0 => {}
1 | 2=> {}
10..=20 => {}
30..=40 | 50 => {}
60..=70 | 75..=80 => {}
x if num < 0 && num%2 == 1 => {}
_ => {}
}
} |
Beta Was this translation helpful? Give feedback.
-
请教一下, matches宏的例子中, assert!(matches!(bar, Some(x) if x > 2)); 这句代码中的matches!(bar, Some(x) if x > 2)怎么理解啊 |
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