[易学易懂系列|rustlang语言|零基础|快速入门|(9)|Control Flows流程控制]

释放双眼,带上耳机,听听看~!

[易学易懂系列|rustlang语言|零基础|快速入门|(9)]

有意思的基础知识

Control Flows


我们今天再来看看流程控制。

条件控制 if-else if -else:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
1/ Simplest Example  
2let team_size = 7;  
3if team_size < 5 {  
4   println!("Small");  
5} else if team_size < 10 {  
6   println!("Medium");  
7} else {  
8   println!("Large");  
9}  
10​  
11// partially refactored code  
12let team_size = 7;  
13let team_size_in_text;  
14if team_size < 5 {  
15   team_size_in_text = "Small";  
16} else if team_size < 10 {  
17   team_size_in_text = "Medium";  
18} else {  
19   team_size_in_text = "Large";  
20}  
21println!("Current team size : {}", team_size_in_text);  
22​  
23//optimistic code  
24let team_size = 7;  
25let team_size_in_text = if team_size < 5 {  
26   "Small" //⭐️no ;  
27} else if team_size < 10 {  
28   "Medium"  
29} else {  
30   "Large"  
31};  
32println!("Current team size : {}", team_size_in_text);  
33​  
34​  
35let is_below_eighteen = if team_size < 18 { true } else { false };
36

模式匹配 match :


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
1let tshirt_width = 20;  
2let tshirt_size = match tshirt_width {  
3   16 => "S", // check 16  
4   17 | 18 => "M", // check 17 and 18  
5   19 ... 21 => "L", // check from 19 to 21 (19,20,21)  
6   22 => "XL",  
7   _ => "Not Available",  
8};  
9println!("{}", tshirt_size); // L  
10​  
11​  
12let is_allowed = false;  
13let list_type = match is_allowed {  
14   true => "Full",  
15   false => "Restricted"  
16   // no default/ _ condition can be skipped  
17   // Because data type of is_allowed is boolean and all possibilities checked on conditions  
18};  
19println!("{}", list_type); // Restricted  
20​  
21​  
22let marks_paper_a: u8 = 25;  
23let marks_paper_b: u8 = 30;  
24let output = match (marks_paper_a, marks_paper_b) {  
25   (50, 50) => "Full marks for both papers",  
26   (50, _) => "Full marks for paper A",  
27   (_, 50) => "Full marks for paper B",  
28   (x, y) if x > 25 && y > 25 => "Good",  
29   (_, _) => "Work hard"  
30};  
31println!("{}", output); // Work hard
32

while:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
1let mut a = 1;  
2while a <= 10 {  
3   println!("Current value : {}", a);  
4   a += 1; //no ++ or -- on Rust  
5}  
6​  
7// Usage of break and continue  
8let mut b = 0;  
9while b < 5 {  
10   if b == 0 {  
11       println!("Skip value : {}", b);  
12       b += 1;  
13       continue;  
14   } else if b == 2 {  
15       println!("Break At : {}", b);  
16       break;  
17   }  
18   println!("Current value : {}", b);  
19   b += 1;  
20}  
21​  
22// Outer break  
23let mut c1 = 1;  
24'outer_while: while c1 < 6 { //set label outer_while  
25   let mut c2 = 1;  
26   'inner_while: while c2 < 6 {  
27       println!("Current Value : [{}][{}]", c1, c2);  
28       if c1 == 2 && c2 == 2 { break 'outer_while; } //kill outer_while  
29       c2 += 1;  
30   }  
31   c1 += 1;  
32}
33

loop


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
1loop {  
2   println!("Loop forever!");  
3}  
4​  
5// Usage of break and continue  
6let mut a = 0;  
7loop {  
8   if a == 0 {  
9       println!("Skip Value : {}", a);  
10       a += 1;  
11       continue;  
12   } else if a == 2 {  
13       println!("Break At : {}", a);  
14       break;  
15   }  
16   println!("Current Value : {}", a);  
17   a += 1;  
18}  
19​  
20// Outer break  
21let mut b1 = 1;  
22'outer_loop: loop { //set label outer_loop  
23 let mut b2 = 1;  
24'inner_loop: loop {  
25   println!("Current Value : [{}][{}]", b1, b2);  
26   if b1 == 2 && b2 == 2 {  
27       break 'outer_loop; // kill outer_loop  
28   } else if b2 == 5 {  
29       break;  
30   }  
31   b2 += 1;  
32}  
33 b1 += 1;  
34}
35

for


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
1for a in 0..10 { //(a = o; a <10; a++) // 0 to 10(exclusive)  
2 println!("Current value : {}", a);  
3}  
4​  
5// Usage of break and continue  
6for b in 0..6 {  
7 if b == 0 {  
8   println!("Skip Value : {}", b);  
9   continue;  
10} else if b == 2 {  
11   println!("Break At : {}", b);  
12   break;  
13}  
14 println!("Current value : {}", b);  
15}  
16​  
17// Outer break  
18'outer_for: for c1 in 1..6 { //set label outer_for  
19'inner_for: for c2 in 1..6 {  
20   println!("Current Value : [{}][{}]", c1, c2);  
21   if c1 == 2 && c2 == 2 { break 'outer_for; } //kill outer_for  
22}  
23}  
24​  
25​  
26// Working with arrays/vectors  
27let group : [&str; 4] = ["Mark", "Larry", "Bill", "Steve"];  
28​  
29for n in 0..group.len() { //group.len() = 4 -> 0..4 ? check group.len()on each iteration  
30 println!("Current Person : {}", group[n]);  
31}  
32​  
33for person in group.iter() { //? group.iter() turn the array into a simple iterator  
34 println!("Current Person : {}", person);  
35}
36

以上,希望对你有用。


1
2
1如果遇到什么问题,欢迎加入:rust新手群,在这里我可以提供一些简单的帮助,加微信:360369487,注明:博客园+rust
2

本人精通java高并发,DDD,微服务等技术实践,以及python,golang技术栈。 本人姓名郭莹城,坐标深圳,前IBM架构师、咨询师、敏捷开发技术教练,前IBM区块链研究小组成员、十四年架构设计工作经验,《区块链核心技术与应用》作者之一, 现有成熟团队提供区块链开发相关业务(公链,交易所,钱包,Dapp,智能合约)。 工作微信&QQ:360369487,交易所开发与区块链钱包开发业务,加我注明:博客园+开发,想学习golang和rust的同学,也可以加我微信,备注:博客园+golang或博客园+rust,谢谢!

参考文章:
https://learning-rust.github.io/docs/a10.control_flows.html

给TA打赏
共{{data.count}}人
人已打赏
安全技术

C/C++内存泄漏及检测

2022-1-11 12:36:11

安全经验

python正则表达式

2021-11-28 16:36:11

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索