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

有意思的基础知识

Control Flows


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

条件控制 if-else if -else:

/ Simplest Example
let team_size = 7;
if team_size < 5 {
   println!("Small");
} else if team_size < 10 {
   println!("Medium");
} else {
   println!("Large");
}

// partially refactored code
let team_size = 7;
let team_size_in_text;
if team_size < 5 {
   team_size_in_text = "Small";
} else if team_size < 10 {
   team_size_in_text = "Medium";
} else {
   team_size_in_text = "Large";
}
println!("Current team size : {}", team_size_in_text);

//optimistic code
let team_size = 7;
let team_size_in_text = if team_size < 5 {
   "Small" //⭐️no ;
} else if team_size < 10 {
   "Medium"
} else {
   "Large"
};
println!("Current team size : {}", team_size_in_text);


let is_below_eighteen = if team_size < 18 { true } else { false };

模式匹配 match :

let tshirt_width = 20;
let tshirt_size = match tshirt_width {
   16 => "S", // check 16
   17 | 18 => "M", // check 17 and 18
   19 ... 21 => "L", // check from 19 to 21 (19,20,21)
   22 => "XL",
   _ => "Not Available",
};
println!("{}", tshirt_size); // L


let is_allowed = false;
let list_type = match is_allowed {
   true => "Full",
   false => "Restricted"
   // no default/ _ condition can be skipped
   // Because data type of is_allowed is boolean and all possibilities checked on conditions
};
println!("{}", list_type); // Restricted


let marks_paper_a: u8 = 25;
let marks_paper_b: u8 = 30;
let output = match (marks_paper_a, marks_paper_b) {
  (50, 50) => "Full marks for both papers",
  (50, _) => "Full marks for paper A",
  (_, 50) => "Full marks for paper B",
  (x, y) if x > 25 && y > 25 => "Good",
  (_, _) => "Work hard"
};
println!("{}", output); // Work hard

while:

let mut a = 1;
while a <= 10 {
   println!("Current value : {}", a);
   a += 1; //no ++ or -- on Rust
}

// Usage of break and continue
let mut b = 0;
while b < 5 {
   if b == 0 {
       println!("Skip value : {}", b);
       b += 1;
       continue;
  } else if b == 2 {
       println!("Break At : {}", b);
       break;
  }
   println!("Current value : {}", b);
   b += 1;
}

// Outer break
let mut c1 = 1;
'outer_while: while c1 < 6 { //set label outer_while
   let mut c2 = 1;
  'inner_while: while c2 < 6 {
       println!("Current Value : [{}][{}]", c1, c2);
       if c1 == 2 && c2 == 2 { break 'outer_while; } //kill outer_while
       c2 += 1;
  }
   c1 += 1;
}

loop

loop {
   println!("Loop forever!");
}

// Usage of break and continue
let mut a = 0;
loop {
   if a == 0 {
       println!("Skip Value : {}", a);
       a += 1;
       continue;
  } else if a == 2 {
       println!("Break At : {}", a);
       break;
  }
   println!("Current Value : {}", a);
   a += 1;
}

// Outer break
let mut b1 = 1;
'outer_loop: loop { //set label outer_loop
 let mut b2 = 1;
'inner_loop: loop {
   println!("Current Value : [{}][{}]", b1, b2);
   if b1 == 2 && b2 == 2 {
       break 'outer_loop; // kill outer_loop
  } else if b2 == 5 {
       break;
  }
   b2 += 1;
}
 b1 += 1;
}

for

for a in 0..10 { //(a = o; a <10; a++) // 0 to 10(exclusive)
 println!("Current value : {}", a);
}

// Usage of break and continue
for b in 0..6 {
 if b == 0 {
   println!("Skip Value : {}", b);
   continue;
} else if b == 2 {
   println!("Break At : {}", b);
   break;
}
 println!("Current value : {}", b);
}

// Outer break
'outer_for: for c1 in 1..6 { //set label outer_for
'inner_for: for c2 in 1..6 {
   println!("Current Value : [{}][{}]", c1, c2);
   if c1 == 2 && c2 == 2 { break 'outer_for; } //kill outer_for
}
}


// Working with arrays/vectors
let group : [&str; 4] = ["Mark", "Larry", "Bill", "Steve"];

for n in 0..group.len() { //group.len() = 4 -> 0..4

[易学易懂系列|rustlang语言|零基础|快速入门|(9)|Control Flows流程控制]的更多相关文章

  1. [易学易懂系列|rustlang语言|零基础|快速入门|系列文章]

    简单易懂的rustlang入门教程. [易学易懂系列|rustlang语言|零基础|快速入门|(1)|开篇] [易学易懂系列|rustlang语言|零基础|快速入门|(2)|VCCode配置] [易学 ...

  2. [易学易懂系列|rustlang语言|零基础|快速入门|(28)|实战5:实现BTC价格转换工具]

    [易学易懂系列|rustlang语言|零基础|快速入门|(28)|实战5:实现BTC价格转换工具] 项目实战 实战5:实现BTC价格转换工具 今天我们来开发一个简单的BTC实时价格转换工具. 我们首先 ...

  3. [易学易懂系列|rustlang语言|零基础|快速入门|(27)|实战4:从零实现BTC区块链]

    [易学易懂系列|rustlang语言|零基础|快速入门|(27)|实战4:从零实现BTC区块链] 项目实战 实战4:从零实现BTC区块链 我们今天来开发我们的BTC区块链系统. 简单来说,从数据结构的 ...

  4. [易学易懂系列|rustlang语言|零基础|快速入门|(26)|实战3:Http服务器(多线程版本)]

    [易学易懂系列|rustlang语言|零基础|快速入门|(26)|实战3:Http服务器(多线程版本)] 项目实战 实战3:Http服务器 我们今天来进一步开发我们的Http服务器,用多线程实现. 我 ...

  5. [易学易懂系列|rustlang语言|零基础|快速入门|(25)|实战2:命令行工具minigrep(2)]

    [易学易懂系列|rustlang语言|零基础|快速入门|(25)|实战2:命令行工具minigrep(2)] 项目实战 实战2:命令行工具minigrep 我们继续开发我们的minigrep. 我们现 ...

  6. [易学易懂系列|rustlang语言|零基础|快速入门|(24)|实战2:命令行工具minigrep(1)]

    [易学易懂系列|rustlang语言|零基础|快速入门|(24)|实战2:命令行工具minigrep(1)] 项目实战 实战2:命令行工具minigrep 有了昨天的基础,我们今天来开始另一个稍微有点 ...

  7. [易学易懂系列|rustlang语言|零基础|快速入门|(23)|实战1:猜数字游戏]

    [易学易懂系列|rustlang语言|零基础|快速入门|(23)|实战1:猜数字游戏] 项目实战 实战1:猜数字游戏 我们今天来来开始简单的项目实战. 第一个简单项目是猜数字游戏. 简单来说,系统给了 ...

  8. [易学易懂系列|rustlang语言|零基础|快速入门|(5)|生命周期Lifetime]

    [易学易懂系列|rustlang语言|零基础|快速入门|(5)] Lifetimes 我们继续谈谈生命周期(lifttime),我们还是拿代码来说话: fn main() { let mut a = ...

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

    [易学易懂系列|rustlang语言|零基础|快速入门|(22)|宏Macro] 实用知识 宏Macro 我们今天来讲讲Rust中强大的宏Macro. Rust的宏macro是实现元编程的强大工具. ...

  10. [易学易懂系列|rustlang语言|零基础|快速入门|(21)|智能指针]

    [易学易懂系列|rustlang语言|零基础|快速入门|(21)|智能指针] 实用知识 智能指针 我们今天来讲讲Rust中的智能指针. 什么是指针? 在Rust,指针(普通指针),就是保存内存地址的值 ...

随机推荐

  1. Linux-ubuntu命令-文件、磁盘管理

    .文件管理 <1>查看文件信息:ls ls是英文单词list的简写,其功能为列出目录的内容,是用户最常用的命令之一,它类似于DOS下的dir命令. Linux文件或者目录名称最长可以有26 ...

  2. C# 给某个方法设定执行超时时间-2

    var response = RunTaskWithTimeout<ReturnType>( (Func<ReturnType>)); /// <summary> ...

  3. JSP指令标签、动作标签

    JSP有三大指令: * page指令   * include指令   * taglib指令    在JSP中没有任何指令是必须的!!! 但基本上每个JSP都是使用page指令! page指令 page ...

  4. 布局复习---BFC

    其实在一开始我是没有BFC的这个概念的,只是知道在浮动过后,后续的元素如果出现问题,就做我们常说的:overflow:hidden.其中的原因还是不甚了解.不是说以前老师没有讲解过,而是以前根本就没有 ...

  5. [不错]A step-by-step guide to enabling security, TLS/SSL, and PKI authentication in Elasticsearch

    Now posted on the Elastic blog December 12, 2018 update: This article has been published on Elastic’ ...

  6. 描述下数据库中的事务--ACID各个的特点

    1. 原子性(Atomicity) 在一个事务内的操作,要么全部成功,要么全部失败. 2. 一致性(Consistency) 数据库从一个一致性状态,转移到另一个一致性状态. 3. 隔离性(Isola ...

  7. No repository found containing: …错误解决

    由于我安装的是Eclipse ForJava Development,无JAVA EE,查找资料后发现可以自己在已有软件的基础上配置,总结如下: >>>>>点开之后,找到 ...

  8. python 并发编程 多线程 开启线程的两种方式

    一 threading模块介绍 multiprocess模块的完全模仿了threading模块的接口,二者在使用层面,有很大的相似性 二 开启线程的两种方式 第一种 每造一个进程,默认有一个线程,就是 ...

  9. Linux内核基础优化

    Linux内核基础优化 net.ipv4.ip_forward = 1 #开启网络转发 net.ipv4.conf.default.rp_filter = 0 #开启代理功能 net.ipv4.con ...

  10. Android 子线程无法刷新UI界面

    问题:在Android开发中,子线程无法直接更改UI界面视图的刷新 这个时候 Handler 起到了至关重要的作用. 简单来说 , Handler就是用来传递消息的. Handler可以当成子线程与主 ...