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

珠海建站模板微网官方网站

珠海建站模板,微网官方网站,系统优化大师下载,ui设计好学吗【Rust】003-基础语法#xff1a;流程控制 文章目录 【Rust】003-基础语法#xff1a;流程控制一、概述二、if 表达式1、语法格式2、多个3、获取表达式的值 三、循环1、loop#xff1a;无限循环#xff0c;可跳出无限循环跳出循环返回值 2、while#xff1a;条件循环…【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给循环加标签 三种循环都支持 当循环存在嵌套关系时break和continue只会对最内层的循环生效。但是有时候我们希望可以对外层的循环做break或者continue这时该怎么办幸运的是Rust 可以给循环加上标签从而break和continue都可以直接操作标签。 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.w-s-a.com/news/401020/

相关文章:

  • 什么网站可以学做西餐个人网站怎么做支付功能
  • 千户微建站平台做网站需要切图吗
  • 织梦cms 学校网站模板网站建设中的问题
  • 山东济南网站建设公司制作wordpress模板教程视频教程
  • 档案网站的建设怎样更新网站内容
  • 网站开发项目规划房地产趋势与前景
  • 网上网站开发这个百度的网站怎么做的
  • 南昌市建设局官方网站网龙网络公司地址
  • 自助建站平台源码公司网站谁负责做
  • 旅游的网站怎么做网站流量的主要来源有
  • 高新网站设计找哪家网络科技有限公司实习报告
  • 专业网站建设质量推荐网络销售是做网站推广
  • 旅游网站建设模板wordpress最好最全的教程
  • 网站站长在哪登陆后台网站设计一年费用
  • 济南比较好的网站建设公司邢台信息网123
  • 双峰网站建设漳州优化网站建设
  • app和网站开发的成本虚拟主机是啥
  • 想做一个自己设计公司的网站怎么做的销售培训
  • 南昌网站建设模板合作凡客app哪去了
  • 有免费做网站的吗建设互联网站
  • 北京市保障房建设投资中心网站淄博哪个网站做房屋出赁好
  • 如何做网站的优化网站开发怎么收费
  • 网站的关键词怎么选择2345实用查询
  • 免费的制作网站做图剪片文案网站app接单
  • 中国有多少网站有多少域名上海网站建设网页制作邢台
  • 网站 数据报表如何做室内设计联盟官方网站入口
  • 怎样建设网站论文合肥做网站的软件公司
  • 收款后自动发货的网站是怎么做的怎么看网站后台网页尺寸
  • 谷歌seo引擎优化宁波seo关键词
  • 外贸网站建设需要注意什么seo课程