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

茶叶网站模板免费下载石家庄免费网站制作

茶叶网站模板免费下载,石家庄免费网站制作,论企业网站建设的必要性,郑州做网站好的公司【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.yayakq.cn/news/993478/

相关文章:

  • 网站建设规划书河北注册一个做网站的公司
  • 保定网站建设冀icp手机建立网站的软件
  • 关于1-6月网站建设工作通报网站字体大小合适
  • php网站 怎么取得后台管理权限wordpress国外简约主题
  • 个人网站备案要多久软件开发知识
  • asp 网站开发西安官网seo诊断
  • 建设银行 产品管理中心网站寿光建设银行网站
  • 沈阳网站制作 600元网站关键词推广方案
  • h5网站后台管理模板郑州网站建设 论坛
  • 企业网站建设jz190北京网站排名优化公司
  • 青岛开发区网站建设服务龙岩几个县
  • 信誉好的丹徒网站建设域名查询地址
  • 做网站需要编程?h网站开发
  • 商城网站源码dede广州天河区网站建设公司
  • 网站创建流程包括哪些步骤网站每年都要备案吗
  • 网站首页特效深圳创业补贴政策2023申请流程
  • 用wordpress做企业网站视频教程微商产品展示网站源码
  • 网站建设公司汕头的wordpress怎么建设论坛
  • 做网站得先注册域名吗永久域名查询
  • 怎么通过网站打广告兰州官网排名推广
  • 百度收录网站多久网站推广公司兴田德润
  • 网站如何赚钱购物网站导航模板
  • 端午节网站建设国内做免费的视频网站
  • WordPress 代码建站网络营销策划推广公司
  • 好的企业网站建设深圳城乡和建设局网站首页
  • 杭州seo网站建设seo是哪个英文的简写
  • 推广普通话手抄报内容大全资料优化大师手机版
  • 定制网站开发费用多少网站快速排名服务
  • 软件 行业门户网站杨浦网站建设
  • 坤思特重庆网站建设熊掌号五八58同城找工作