pattern-match/match-iflet #177
Replies: 38 comments 9 replies
-
“你可以在这里找到答案(在 solutions 路径下)”,这句话的超链接指向有问题 |
Beta Was this translation helpful? Give feedback.
-
练习6中为啥我这样写 会报错 error[E0425]: cannot find value
|
Beta Was this translation helpful? Give feedback.
-
小白求教,课后题第三题中Message枚举中的Move后面为啥跟的是花括号 |
Beta Was this translation helpful? Give feedback.
-
在第四题中,我遇到一个想不明白的问题。 fn main() {
let alphabets = ['a', 'E', 'Z', '0', 'x', '9' , 'Y'];
// 使用 `matches` 填空
for ab in alphabets {
// 这里为什么会断言通过呢
assert!(matches!(ab , i32 ));
}
}
|
Beta Was this translation helpful? Give feedback.
-
感觉rust中的 |
Beta Was this translation helpful? Give feedback.
-
done done |
Beta Was this translation helpful? Give feedback.
-
fn main() {
// 1.
// let dire = Direction::South;
// match dire {
// Direction::East => println!("East"),
// Direction::North | Direction::South => println!("North or South"),
// Direction::West => println!("West"),
// }
// 2.
// let boolean = true;
// let binary = match boolean {
// true => 1,
// false => 0,
// };
// println!("binary: {}", binary);
// 3.
// let msgs = [
// Message::Quit,
// Message::Move{x: 1, y: 3},
// Message::ChangeColor(255, 255, 0)
// ];
// for msg in msgs {
// // println!("{:?}", msg);
// show_message(msg)
// }
// 4.
// let alphabets = ['a', 'E', 'Z', '0', 'x', '9', 'Y'];
// for ab in alphabets {
// assert!(matches!(ab, 'a' | 'E' | 'Z' | 'Y' | '0' | '9' | 'x'));
// }
// 5.
let mut count = 0;
let v = vec![MyEnum::Foo, MyEnum::Bar, MyEnum::Foo];
for e in v {
// if e == MyEnum::Foo {
if matches!(e, MyEnum::Foo) {
count += 1;
}
}
assert_eq!(count, 2);
// 6.
// let o = Some(7);
// // match o {
// // Some(i) => {
// // println!("This is really long string and '{:?}'", i);
// // },
// // _ => {}
// // };
// if let Some(i) = o {
// println!("This is really long string and '{:?}'", i);
// }
// 7.
// let a = Foo::Bar(1);
// if let Foo::Bar(i) = a { // 不太明白这个i是个什么变量? 在模式匹配中进行定义,不需要单独的进行定义
// println!("foobar values is {}", i);
// }
// 8.
// let a = Foo::Qux(10);
// // if let Foo::Bar = a {
// // println!("match foo::bar");
// // } else if let Foo::Baz = a {
// // println!("match foo::baz");
// // } else {
// // println!("match others");
// // }
// match a {
// Foo::Bar => println!("match foo::bar"),
// Foo::Baz => println!("match foo::baz"),
// _ => println!("match others"),
// };
// 9.
let age = Some(30);
if let Some(a) = age {
assert_eq!(a, 30);
}
match age {
Some(a) => println!("age is {}", a),
_ => {}
}
println!("Hello, world!");
}
// 8.
enum Foo {
Bar,
Baz,
Qux(u32)
}
// 7.
// enum Foo {
// Bar(u8)
// }
// 5.
enum MyEnum {
Foo,
Bar
}
// 3.
// #[derive(Debug)]
// enum Message {
// Quit,
// Move { x: i32, y: i32 },
// Write(String),
// ChangeColor(i32, i32, i32),
// }
// fn show_message(msg: Message) {
// match msg {
// Message::Move{x: a, y: b} => {
// assert_eq!(a, 1);
// assert_eq!(b, 3);
// },
// Message::ChangeColor(_, g, b) => {
// assert_eq!(g, 255);
// assert_eq!(b, 0);
// },
// _ => println!("no data in these variants")
// }
// }
// 1.
// enum Direction {
// East,
// West,
// South,
// North,
// } |
Beta Was this translation helpful? Give feedback.
-
Done. |
Beta Was this translation helpful? Give feedback.
-
it seems I am learning!!! |
Beta Was this translation helpful? Give feedback.
-
第四题也可以这样吧: fn main() {
} |
Beta Was this translation helpful? Give feedback.
-
match 里面的 target = xxx , 这个逗号咋有时候有有时候没有,都可以。。这么随意 |
Beta Was this translation helpful? Give feedback.
-
pattern-match/match-iflet
Learning Rust By Practice, narrowing the gap between beginner and skilled-dev with challenging examples, exercises and projects.
https://zh.practice.rs/pattern-match/match-iflet.html
Beta Was this translation helpful? Give feedback.
All reactions