首页 > 其他分享 >Rust流程控制__Control Flow

Rust流程控制__Control Flow

时间:2024-04-12 15:56:11浏览次数:24  
标签:__ Control Flow number let Rust println loop count

在Rust中常用于流程控制的代码就是if expressions & loops

if expressions

if expressions以关键字if开始,后面跟一个条件(布尔类型)。Optionally,we can also include an else expression, which we choose to do here, to give the program an alternative block of code to execute should the condition evaluate to false. if you don't provide an else expression and the condition is false, the program will just skip the if block and move on to next bit of code.

Because if is an expression, we can use it on the right side of a let statement to assign the outcome to a variable.由于if是表达式,因此我们可以将其返回的结果赋值给一个变量。

    // if expressions
    let number = 3;
    if number < 5 {
        println!("condition is true");
    } else {
        println!("condition is false");
    }

    
    let condition = true;
    let number = if condition { 5 } else { 6 };

    println!("The value of number is: {number}");

 

Repetition with Loops

It's often useful to execute a block of code more than once. For this task, Rust provides several loops, which will run through the code inside the loop body to the end and then start immediately back at the beginning. Rust has three kinds of loops: loop, while and for.

The loop keyword tells Rust to execute a block of code over and over again forever or until you explicitly tell it to stop.

fn main() {
    // loop
    let mut counter = 0;

    let result = loop {
        counter += 1;

        if counter == 10 {
            // break可以结束循环并返回结果
            break counter * 2;
        }
    };

    println!("The result is {result}");
    
    // ----------------------------------------------------------------

    let mut count = 0;
    // 循环标签loop label,配合break使用,用以告知Rust结束的是哪一个循环
    // 在单重loop中,通常不使用loop label,break关键字单独使用时,默认结束当前循环体的循环
    'outer: loop {
        println!("count = {count}");
        let mut remaining = 10;

        'inner: loop {
            println!("remaining = {remaining}");
            if remaining == 9 {
                break 'inner;
            }
            if count == 2{
                break 'outer;
            }
            remaining -= 1;
        }
        count += 1;
    }
    println!("End count = {count}");
}

 

Conditional Loops with while

条件循环while

fn main() {
    let mut number = 3;

    while number != 0 {
        println!("{number}!");

        number -= 1;
    }

    println!("LIFTOFF!!!");
}

 

Looping Through a Collection with for

遍历集合的循环for

fn main() {
    let a = [10, 20, 30, 40, 50];

    for element in a {
        println!("the value is: {element}");
    }
}

 

标签:__,Control,Flow,number,let,Rust,println,loop,count
From: https://www.cnblogs.com/ashet/p/18131492

相关文章

  • SOLIDWORKS模板批量修改工具 慧德敏学
    SOLIDWORKS批量修改模板插件-SolidKits.BOMs工具可实现工程图模板的批量替换,单位系统的批量修改,批量定义模型材质等功能。操作简单快捷,只需要提前打开SOLIDWORKS软件,执行后程序会自动完成所有替换操作。使用SOLIDWORKS绘制工程图之前,必须要选择工程图模板,模板中我们会定义好图幅......
  • redis基础
    redis数据类型redis可以理解成一个全局的大字典,key就是数据的唯一标识符。根据key对应的值不同,可以划分成5个基本数据类型。redis={"name":"yuan","scors":["100","89","78"],"info":{"name":"rain"......
  • 实验一
    1、对比分析墨刀、Axure、Mockplus等原型设计工具的各自的适用领域及优缺点(至少3条)。Axure优点:1、强大的编辑功能,便于制作素材库。2、快速的复制粘贴,素材库和原型库丰富。3、项目共享功能,方便同事间同步工作,保留所有工作历史。Axure缺点:1、相对于其他工具,AxureRP学习曲线......
  • lloyd-max 最优标量量化算法分析
    变限积分求导公式假设有函数定义为:\[K(x)=\int_{\phi(x)}^{\Psi(x)}f(t)dt\\\frac{dK(x)}{dx}=f[\Psi(x)]\Psi(x)^{\prime}-f[\phi(x)]\phi(x)^{\prime}\]量化失真与最优标量量化对于N个量化区间的失真定义为:\[D=\sum_{i=1}^{N}(\int_{t_i}^{t_{i+1}}(x-\hat{x_i......
  • windows和Linux下路径表示
    reference一、\(Windows\)下的路径表示由于\(DOS\)原因,过去的\(windows\)路径表示采用反斜杠\,而路径字符串由于反斜杠的转义字符,因此需要用双反斜杠\\。\(Windows\)的根据路为磁盘号,后面跟:path如今的\(Windows\)内核在处理路径时同时支持正斜杠和反斜杠。但有时候......
  • nacos启用鉴权后curl调用接口
    1.通过用户名密码获取token密码尽量不要带特殊字符,否则可能识别错误/#curl-XPOST'http://192.168.60.181:8848/nacos/v1/auth/login'-d'username=nacos&password=nacos'{"accessToken":"eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJuYWNvcyIsImV4cCI6MTcxMjkyNDc......
  • C++陷阱—指定的返回类型的函数实际没有返回时会发生什么
    当一个string变量作为左值接收函数返回,当函数没有正确返回时,该string变量被如何构造?请看如下代码:#include<iostream>#include<string.h>usingnamespacestd;stringfoo(){if(0){return"youget";}}intmain(intargc,char**argv){......
  • ZCMU-1053
    比较简单记录一下主要感觉它这个题目没说清楚,题目要求:先有n,接着给出长度为n的标准组,然后给出猜测组,输出的两个数一个是有多少个是相对应的既相同坐标其数值也相同,后一个是两个都有但是位置不同(不含已经相同的)我觉得它少了一类个例子:类似于123436133343思路:用......
  • 电子秤解决方案开发流程
    一、引言电子秤作为一种便携式电子称重设备,因其小巧、便携、精度高等特点,广泛应用于各种需要精确称重的场景。本文将对电子秤的方案技术进行分析,包括其基本原理、硬件构成、软件设计等方面,旨在为相关技术人员提供参考。二、电子秤的基本原理电子秤的基本原理是......
  • C++编译器对溢出的默认处理
    C++编译器对溢出的默认处理在算数运算中,有一个比较头疼又必须要处理的事情:“溢出”,当我们有所疏忽,没有对溢出的情况做处理时,在我们不知情下就会产生很诡异的bug!那么当我们没有做溢出处理时,编译器的默认处理方式是什么呢?下面我们探究一下这个问题。测试环境Linux4.15.0#16.0......