[易学易懂系列|rustlang语言|零基础|快速入门|(9)|Control Flows流程控制]
[易学易懂系列|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流程控制]的更多相关文章
- [易学易懂系列|rustlang语言|零基础|快速入门|系列文章]
简单易懂的rustlang入门教程. [易学易懂系列|rustlang语言|零基础|快速入门|(1)|开篇] [易学易懂系列|rustlang语言|零基础|快速入门|(2)|VCCode配置] [易学 ...
- [易学易懂系列|rustlang语言|零基础|快速入门|(28)|实战5:实现BTC价格转换工具]
[易学易懂系列|rustlang语言|零基础|快速入门|(28)|实战5:实现BTC价格转换工具] 项目实战 实战5:实现BTC价格转换工具 今天我们来开发一个简单的BTC实时价格转换工具. 我们首先 ...
- [易学易懂系列|rustlang语言|零基础|快速入门|(27)|实战4:从零实现BTC区块链]
[易学易懂系列|rustlang语言|零基础|快速入门|(27)|实战4:从零实现BTC区块链] 项目实战 实战4:从零实现BTC区块链 我们今天来开发我们的BTC区块链系统. 简单来说,从数据结构的 ...
- [易学易懂系列|rustlang语言|零基础|快速入门|(26)|实战3:Http服务器(多线程版本)]
[易学易懂系列|rustlang语言|零基础|快速入门|(26)|实战3:Http服务器(多线程版本)] 项目实战 实战3:Http服务器 我们今天来进一步开发我们的Http服务器,用多线程实现. 我 ...
- [易学易懂系列|rustlang语言|零基础|快速入门|(25)|实战2:命令行工具minigrep(2)]
[易学易懂系列|rustlang语言|零基础|快速入门|(25)|实战2:命令行工具minigrep(2)] 项目实战 实战2:命令行工具minigrep 我们继续开发我们的minigrep. 我们现 ...
- [易学易懂系列|rustlang语言|零基础|快速入门|(24)|实战2:命令行工具minigrep(1)]
[易学易懂系列|rustlang语言|零基础|快速入门|(24)|实战2:命令行工具minigrep(1)] 项目实战 实战2:命令行工具minigrep 有了昨天的基础,我们今天来开始另一个稍微有点 ...
- [易学易懂系列|rustlang语言|零基础|快速入门|(23)|实战1:猜数字游戏]
[易学易懂系列|rustlang语言|零基础|快速入门|(23)|实战1:猜数字游戏] 项目实战 实战1:猜数字游戏 我们今天来来开始简单的项目实战. 第一个简单项目是猜数字游戏. 简单来说,系统给了 ...
- [易学易懂系列|rustlang语言|零基础|快速入门|(5)|生命周期Lifetime]
[易学易懂系列|rustlang语言|零基础|快速入门|(5)] Lifetimes 我们继续谈谈生命周期(lifttime),我们还是拿代码来说话: fn main() { let mut a = ...
- [易学易懂系列|rustlang语言|零基础|快速入门|(22)|宏Macro]
[易学易懂系列|rustlang语言|零基础|快速入门|(22)|宏Macro] 实用知识 宏Macro 我们今天来讲讲Rust中强大的宏Macro. Rust的宏macro是实现元编程的强大工具. ...
- [易学易懂系列|rustlang语言|零基础|快速入门|(21)|智能指针]
[易学易懂系列|rustlang语言|零基础|快速入门|(21)|智能指针] 实用知识 智能指针 我们今天来讲讲Rust中的智能指针. 什么是指针? 在Rust,指针(普通指针),就是保存内存地址的值 ...
随机推荐
- 浏览器端-W3School-JavaScript-HTML DOM:HTML DOM Attribute 对象
ylbtech-浏览器端-W3School-JavaScript-HTML DOM:HTML DOM Attribute 对象 1.返回顶部 1. HTML DOM Attribute 对象 HT ...
- ColorPicker 颜色选择器
用于颜色选择,支持多种格式. 基础用法 使用 v-model 与 Vue 实例中的一个变量进行双向绑定,绑定的变量需要是字符串类型. <div class="block"&g ...
- Reactjs之Axios、fetch-jsonp获取后台数据
1.新增知识点 /** Axios获取服务器数据(无法跨域,只能让后台跨域获取数据) react获取服务器APi接口的数据: react中没有提供专门的请求数据的模块.但是我们可以使用任何第三方请求数 ...
- React 事件对象、键盘事件、表单事件、ref获取dom节点、react实现类似Vue双向数据绑定
1.案例实现代码 import React, { Component } from 'react'; /** * 事件对象.键盘事件.表单事件.ref获取dom节点.react实现类似Vue双向数据绑 ...
- Windows环境下Mysql 5.7读写分离之使用mysql-proxy练习篇
本文使用mysql-proxy软件,结合mysql读写分离,实现实战练习. 前期准备: 三台机器: 代理机,IP:192.168.3.33 mysql Master,IP:192.168.3.32 m ...
- Web09_MySQL多表&JDBC
使用JDBC发送insert语句完成单表[添加]操作 使用JDBC发送update语句完成单表[更新]操作 使用JDBC发送delete语句完成单表[删除]操作 使用JDBC发送select语句完成单 ...
- java:LeakFilling(Springmvc)
1.后台可以同时多个对象接收前端页面的值:(如图两个都打印了) 2.参数绑定的注解,通过该注解可以解决参数名称与controller中形参名称不一致的问题: @RequestParam(name=&q ...
- cocos2dx[3.2](8) 数学类Vec2/Size/Rect
数学类Vec2.Size.Rect,是cocos2dx中比较常用的类. 比如设置图片位置,设置图片大小,两图片的碰撞检测等等. 比起2.x版本,在3.x中本质上其实没有太大的变化,主要的变化就是将全局 ...
- ansible自动化运维管理工具
1.Ansible介绍 1)Ansible:Ansible的核心程序 2)Host Inventory:(默认路径:/etc/ansible/hosts)记录了每一个由Ansible管理的主机信息,信 ...
- CentOS7Linux中服务器LVS负载均衡、高可用集群搭建(NAT、DR);
目录 集群 声明 集群概念 集群特性 Web服务器并发相应瓶颈 集群的分类 LB实现方法: LVS集群 负载调度器 服务器池 共享存储 LVS负载均衡的三种模式 负载均衡 集群 声明 文档不断更新中. ...