当前位置: 首页 > news >正文

大连网站搭建与推广dede中英文网站

大连网站搭建与推广,dede中英文网站,如何在360网站上做软文推广,瑞安做网站的公司【Rust】003-基础语法:流程控制 文章目录 【Rust】003-基础语法:流程控制一、概述二、if 表达式1、语法格式2、多个3、获取表达式的值 三、循环1、loop:无限循环,可跳出无限循环跳出循环返回值 2、while:条件循环&…

【Rust】003-基础语法:流程控制

文章目录

  • 【Rust】003-基础语法:流程控制
  • 一、概述
  • 二、if 表达式
    • 1、语法格式
    • 2、多个
    • 3、获取表达式的值
  • 三、循环
    • 1、loop:无限循环,可跳出
      • 无限循环
      • 跳出循环
      • 返回值
    • 2、while:条件循环,可跳出
    • 3、for:常用于访问集合
      • 访问集合:while 示例
      • 访问集合:for 示例
      • 对一个 range 进行循环
    • 4、labels:给循环加标签

一、概述

控制流是编程语言的一个重要概念。程序员通过控制流可以控制哪些代码要执行。在Rust中,最常见的两种控制流结构是if表达式和循环。

二、if 表达式

1、语法格式

这里的 condition 必须是 bool 类型

if condition {// code to execute if the condition is true
} else {// code to execute if the condition is false
}

2、多个

if condition1 {// code to execute if condition1 is true
} else if condition2 {// code to execute if condition1 is false and condition2 is true
} else {// code to execute if both conditions are false
}

3、获取表达式的值

正如这一小节的标题所说,if其实是一个表达式,具有返回值。

需要注意的是,if分支和else分支的返回值必须是同一类型

fn main() {let temperature = 20;let weather = if temperature >= 25 {"hot"} else {"cool"};println!("The weather today is {}.", weather);
}

三、循环

Rust中提供了三种循环方式,loopwhilefor

1、loop:无限循环,可跳出

无限循环

loop关键字会创建一个无限循环

loop {// code to execute repeatedly
}

跳出循环

想要从循环中跳出,需要配合break关键词使用,下面的代码也展示了 continue 的用法!

let mut counter = 0;loop {counter += 1;if counter < 5 {continue;}println!("Hello, world!");if counter >= 5 {break;}
}

返回值

fn main() {let target = 10;let mut sum = 0;let mut counter = 1;let result = loop {sum += counter;if sum >= target {break counter; // The value of counter will be returned from the loop as a result}counter += 1;};println!("The first number whose sum of all previous numbers is greater than or equal to {} is {}.", target, result);
}

2、while:条件循环,可跳出

while condition {// code to execute while the condition is true
}

3、for:常用于访问集合

访问集合:while 示例

fn main() {let numbers = [1, 2, 3, 4, 5];let mut index = 0;while index < numbers.len() {println!("The value is: {}", numbers[index]);index += 1;}
}

访问集合:for 示例

fn main() {let numbers = [1, 2, 3, 4, 5];for number in numbers {println!("The value is: {}", number);}
}

对一个 range 进行循环

这里的1..=3表示[1,3]这个区间的整数。如果是左闭右开,要写成1..3

fn main() {for x in 1..=3 {println!("x: {}", x);}
}

4、labels:给循环加标签

三种循环都支持!

当循环存在嵌套关系时,breakcontinue只会对最内层的循环生效。但是有时候我们希望可以对外层的循环做break或者continue,这时该怎么办?幸运的是,Rust 可以给循环加上标签,从而breakcontinue都可以直接操作标签。

fn main() {let x = 1;'outer: loop {let mut y = 1;'inner: loop {if y == 3 {y += 1;continue 'inner; // Skips to the next iteration of the 'inner loop}println!("x: {}, y: {}", x, y);y += 1;if y > 5 {break 'outer; // Breaks out of the 'inner loop}}}
}

带返回值

fn main() {let x = 1;let z = 'outer: loop {let mut y = 1;'inner: loop {if y == 3 {y += 1;continue 'inner; // Skips to the next iteration of the 'inner loop}println!("x: {}, y: {}", x, y);y += 1;if y > 5 {break 'outer y; // Breaks out of the 'inner loop}}};println!("z: {}", z);
}
http://www.15wanjia.com/news/156259.html

相关文章:

  • 自动做reference的网站html前端网页模板
  • opencms做网站 谁杨凌开发建设局网站
  • 东莞企业网站后缀中山网站建设文化服务
  • 佛山网站网站建设平台期怎么突破
  • 泉州建网站自学编程入门先学什么
  • 安卓手机网站开发如何推广自己的网站和产品
  • 找图片素材网站一级a做爰片免费网站短视频播放
  • 上海 网站设计公司推广的公司
  • 湖北省建设厅造价官方网站三星杭州 两学一做网站
  • logo制作网站免费互联网站平台有哪些
  • 自己做网站流程网站排名关键词
  • 截图京东图片做网站汇中建设 官方网站
  • 东莞市做网站最新新闻热点事件2024年
  • 有哪些做高考模拟卷的网站上海网站建设网页设计
  • 七星迪曼网站建设衡阳网站建设怎样收费
  • 徐汇网站开发培训wordpress手机版本
  • 大连市的网络平台有几家佛山优化网站推广
  • hyip网站开发广东网站建设开发
  • 济南做网站公司电话设计赚钱的平台有哪些
  • seo网站整站优化洛阳有哪些做网站的公司
  • 如何修改响应式网站模板怎样更新网站
  • 网站开发流程抚州wordpress标题属性
  • 怎么建好网站3d建模可以自学吗
  • 模板商城建站公司简介介绍
  • 创网站多少钱网上有哪些接单做效果图的网站
  • 建立网站的程序wordpress the7 下载
  • 网站搭建代码手机网站底部导航菜单
  • 永嘉专业网站建设团队18种禁用软件黄app入口
  • 中国十大流量网站wordpress app源码
  • 一个人做网站好做吗白酒网站模板