Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

pattern-match/match-iflet #177

giscus[bot] bot announced in Book Comments
Mar 28, 2022 · 55 comments · 9 replies
Discussion options

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

You must be logged in to vote

Replies: 55 comments 9 replies

Comment options

done

You must be logged in to vote
0 replies
Comment options

done

You must be logged in to vote
0 replies
Comment options

"你可以在这里找到答案(在 solutions 路径下)",这句话的超链接指向有问题

You must be logged in to vote
0 replies
Comment options

done

You must be logged in to vote
0 replies
Comment options

Done.

You must be logged in to vote
0 replies
Comment options

done

You must be logged in to vote
0 replies
Comment options

Done.

You must be logged in to vote
1 reply
Comment options

2022年11月15日 Done.

Comment options

down

You must be logged in to vote
0 replies
Comment options

练习6中为啥我这样写 会报错 error[E0425]: cannot find value i in this scope

fn main() {
 let o = Some(7);
 if let o = Some(i) {
 println!("This is a really long string and `{:?}`", i);
 }
}
You must be logged in to vote
2 replies
Comment options

單純的語法誤解

if let Some(`新的(解開包裝後的)變數名稱`) = `目標解開包裝的變數` {
...snip
}
正確答案在這邊
https://github.com/sunface/rust-by-practice/blob/master/solutions/pattern-match/match.md
Comment options

fn main() {
 let o = Some(7);
 if let Some(i) = o {
 println!("This is a really long string and `{:?}`", i);
 }
}
Comment options

done

You must be logged in to vote
0 replies
Comment options

done

You must be logged in to vote
0 replies
Comment options

小白求教,课后题第三题中Message枚举中的Move后面为啥跟的是花括号

You must be logged in to vote
1 reply
Comment options

Move后是一个匿名结构体,所以是花括号。

Comment options

在第四题中,我遇到一个想不明白的问题。

fn main() {
 let alphabets = ['a', 'E', 'Z', '0', 'x', '9' , 'Y'];
 // 使用 `matches` 填空
 for ab in alphabets {
 // 这里为什么会断言通过呢
 assert!(matches!(ab , i32 ));
 }
} 

matches!(ab , i32 )返回的结果是true,为什么不是false呢?

You must be logged in to vote
2 replies
Comment options

这里的i32应该只是作为一个名称而非类型名吧, 就和换成a, b之类的名称一样含义。

Comment options

这里还会有个警告 ^^^ help: if this is intentional, prefix it with an underscore: _i32 让你不要用i32作为名称

Comment options

lala~

You must be logged in to vote
0 replies
Comment options

感觉rust中的字符范围表达式应该是按照ascii码表来看是否连续的
第四题中 matches!(ab, ' '..='~') 也是没问题的

You must be logged in to vote
2 replies
Comment options

个人感觉应该是按照 charUTF-8标准 映射出的 bytes范围 来判断

Comment options

个人感觉应该是按照 charUTF-8标准 映射出的 bytes范围 来判断

确实,是我之前理解有问题,应该是按照char的对应unicode编码来进行范围判断的,教程里面已经明确说了char是按unicode编码的,只是很多unicode字符平常实际不怎么用就是了😂

Comment options

终于在这章领悟到变量解构了

You must be logged in to vote
0 replies
Comment options

done

You must be logged in to vote
0 replies
Comment options

done

You must be logged in to vote
0 replies
Comment options

Done!

You must be logged in to vote
0 replies
Comment options

感觉这节相对简单点

You must be logged in to vote
0 replies
Comment options

完成

You must be logged in to vote
0 replies
Comment options

done

You must be logged in to vote
0 replies
Comment options

done

You must be logged in to vote
0 replies
Comment options

Done ✅
Day 7

You must be logged in to vote
0 replies
Comment options

done

You must be logged in to vote
0 replies
Comment options

第5题好像还能这样写

enum MyEnum {
 Foo,
 Bar
}
fn main() {
 let mut count = 0;
 let v = vec![MyEnum::Foo,MyEnum::Bar,MyEnum::Foo];
 for e in v {
 if let MyEnum::Foo = e { // 修复错误,只能修改本行代码
 count += 1;
 }
 }
 assert_eq!(count, 2);
}
You must be logged in to vote
0 replies
Comment options

done

You must be logged in to vote
0 replies
Comment options

Done

You must be logged in to vote
0 replies
Comment options

done

You must be logged in to vote
0 replies
Comment options

done

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

AltStyle によって変換されたページ (->オリジナル) /