Replies: 40 comments 45 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.
-
很灵活,很强大 |
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.
-
懵了😂 |
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.
-
解构不定长数组少了一种用法,这种用法对于减少嵌套层数很有用 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.
-
最后@绑定那里
是不是也可以用
达到同样的目的,只不过啰嗦一点。 |
Beta Was this translation helpful? Give feedback.
-
严格一点,RGB 的定义就是 3 x 8 ,定义成 i32 是否合适 |
Beta Was this translation helpful? Give feedback.
-
这已经是我两年内第五次学习Rust了,都是从头重新学的2333。上班族太苦逼了 |
Beta Was this translation helpful? Give feedback.
-
那我是不是可以 let y @ 5 来进行变量绑定? |
Beta Was this translation helpful? Give feedback.
-
可惜牛客的rust才到1.44 |
Beta Was this translation helpful? Give feedback.
-
输出:
导致最后打印的原因是:x was not 10 :(Point { x: 10, y: 23 })
|
Beta Was this translation helpful? Give feedback.
-
特性太多了,记不住那么多 |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
https://course.rs/basic/match-pattern/all-patterns.html
Beta Was this translation helpful? Give feedback.
All reactions