Replies: 33 comments 39 replies
-
这里表达错误了,第二个分支指定匹配 x 为 0 的 Point,而不是匹配y不为0,x为0的Point,可以匹配y为0的Point,建议更改一下。 |
Beta Was this translation helpful? Give feedback.
-
x=0,y=0 会匹配第一个分支,匹配不到第二个 |
Beta Was this translation helpful? Give feedback.
-
@绑定可能是所有模式匹配中最难记忆的一个。
我现在这样助记: @ 符号右侧的 |
Beta Was this translation helpful? Give feedback.
-
能问一下吗,在match中第一个分支中x匹配了some(s)此时x的所有权转移到了s中了吗?如果转移到了s上那么因为匹配守卫的关系,这一分支就不匹配了,这时应该匹配第二还是第三个分支。 |
Beta Was this translation helpful? Give feedback.
-
是不支持吗???? error[E0658]: pattern bindings after an |
Beta Was this translation helpful? Give feedback.
-
这里是不是说的逻辑有问题? x不可能同时为Some(5)和None吧 |
Beta Was this translation helpful? Give feedback.
-
属性条件与属性变量 复杂类型 | 基本类型 数据的绑定 任何一个匹配都可以写成这种形式: |
Beta Was this translation helpful? Give feedback.
-
let arr: &[u16] = &[114, 514]; if let [x, ..] = arr { if let &[.., y] = arr { 这么臭的数组有匹配的必要吗(半恼) |
Beta Was this translation helpful? Give feedback.
-
if let 好像没有守卫匹配的语法 |
Beta Was this translation helpful? Give feedback.
-
let arr: [u16; 2] = [114, 514]; 哈哈 |
Beta Was this translation helpful? Give feedback.
-
enum Message { let msg = Message::Hello { id: 5 }; match msg { fn main() { |
Beta Was this translation helpful? Give feedback.
-
enum Message {
Hello { id: i32 },
}
let msg = Message::Hello { id: 5 };
match msg {
Message::Hello { id: id_variable @ 3..=7 } => {
println!("Found an id in range: {}", id_variable)
},
Message::Hello { id: 10..=12 } => {
println!("Found an id in another range")
},
Message::Hello { id } => {
println!("Found some other id: {}", id)
},
}
上面这个跟我下面这个用守卫模式有啥区别吗?
enum Message {
Hello {id:i32},
}
fn main() {
let msg = Message::Hello { id: 5 };
match msg {
Message::Hello {id} if id>2 => {
print!("{}\n", id)
},
_ => {
println!("Nothing")
}
}
} |
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.
-
这一章更多是字典书,需要的时候再回来查阅! |
Beta Was this translation helpful? Give feedback.
-
解构数组这一段建议调整一下,强调一下区别与原因。 😂这里想了好久 |
Beta Was this translation helpful? Give feedback.
-
炸裂,这个 #[derive(Debug)]
struct Point {
x: i32,
y: i32,
}
fn main() {
// 绑定@的基本使用
let p = Point { x: 3, y: 2 };
match p {
// 将范围可能是 10~20 的 y 绑定到 y1,此时的匹配值是: x任意值,y的取值为10~20
Point { x, y: y1 @ 10..=20 } => {
println!("x: {}, y: {}", x, y1);
}
_ => {
println!("not match");
}
}
let p1 = Point { x: 33, y: 22 };
// let Point { x, y } = p1; // 解构
let p_at @ Point { x: x1, y: y1 } = p1; //这里首先对 p1 进行解构,将33绑定到x1变量,22绑定到y1变量,然后再通过@绑定到p_at
println!("x1 = {}, y1 = {}", x1, y1); // 这里的x1,y1是上一行解构出来的
println!("p_at = {:?}", p_at);
let tuple = (String::from("value1"), String::from("value2"));
let t_at @ (t1, t2) = &tuple;
// let t_ref = &tuple;
// let (t1, t2) = t_ref;
println!("t1 = {}, t2 = {}", t1, t2);
println!("t_at.0 = {}, t_at.1 = {}", t_at.0, t_at.1);
// println!("t_ref.0 = {}, t_ref.1 = {}", t_ref.0, t_ref.1);
} |
Beta Was this translation helpful? Give feedback.
-
建议将@句法放到 匹配命名变量 后面, 把 |
Beta Was this translation helpful? Give feedback.
-
好难啊,记不住都~ |
Beta Was this translation helpful? Give feedback.
-
解构不定长数组少了一种用法,这种用法对于减少嵌套层数很有用 fn main() {
let arr: &[i32] = &[1, 2];
let [x, y] = arr else {
panic!();
};
println!("x:{x}, y:{y}");
} |
Beta Was this translation helpful? Give feedback.
-
解构结构体和元组那里的例子容易误会,且不好理解,建议改成这样: |
Beta Was this translation helpful? Give feedback.
-
fn main() {
let x = Some(5);
let y = 10;
match x {
Some(50) => println!("Got 50"),
Some(y) => println!("Matched, y = {:?}", y),
_ => println!("Default case, x = {:?}", x),
}
println!("at the end: x = {:?}, y = {:?}", x, y);
} 这段代码中 但是如果我希望打印出 |
Beta Was this translation helpful? Give feedback.
-
fn main() {
} "如果 x 的值是 None 而不是 Some(5),头两个分支的模式不会匹配,所以会匹配模式 _。这个分支的模式中没有引入变量 x,所以此时表达式中的 x 会是外部没有被遮蔽的 x,也就是 None" 这句话怎么理解? 之前提到了y是新的变量,会匹配x的所有值,为何不是打印println!("Matched, y = {:?}", y)? 是不是因为None为Option中的一个特殊值? |
Beta Was this translation helpful? Give feedback.
-
let arr: &[u16] = &[114, 514];
if let [x, ..] = arr {
assert_eq!(x, &114);
} 这里已知 arr 是一个 |
Beta Was this translation helpful? Give feedback.
-
https://course.rs/basic/match-pattern/all-patterns.html
Beta Was this translation helpful? Give feedback.
All reactions