首页 > 其他分享 >RUST——控制流(if/loop/while)

RUST——控制流(if/loop/while)

时间:2023-04-11 09:45:19浏览次数:31  
标签:count 控制流 number while let fn println main loop

1. if语句

下面看一个示例:

fn main() {
    let number = 6;
    if number % 4 == 0 {
        println!("number is divisible by 4");
    } else if number % 3 == 0 {
        println!("number is divisible by 3");
    } else if number % 2 == 0 {
        println!("number is divisible by 2");
    } else {
        println!("number is not divisible by 4, 3, or 2");
    }
}

(1) 需要注意的是,RUST与cpp不同,不可以将整型识别为bool,所以下面的语句会报错:

fn main() {
    let number =3;
    if number{
        println!("condition is true");
    }else{
        println!("condition is false");
    }
}


(2)另一个需要注意的点是,if多个分支返回的值要是同类型

fn main() {
    let condition = true;

    let number = if condition { 5 } else { "six" };

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

因为RUST需要在编译的时候确定number的类型,如果类型无法确定后续对代码的保证会降低

2. 循环

2.1 loop

一个简单的例子:

fn main() {
    loop {
        println!("again!");
    }
}

比较特别的一个点是,允许循环标签来在内循环中断外部循环

fn main() {
    let mut count = 0;
    'counting_up: loop {
        println!("count = {count}");
        let mut remaining = 10;

        loop {
            println!("remaining = {remaining}");
            if remaining == 9 {
                break;
            }
            if count == 2 {
                break 'counting_up;
            }
            remaining -= 1;
        }

        count += 1;
    }
    println!("End count = {count}");
}

2.2 while

while的用法和其他语言类似,同样需要注意表达式的返回值必须为布尔值

fn main() {
    let mut number = 3;
    while number != 0 {
        println!("{number}!");
        number -= 1;
    }
    println!("LIFTOFF!!!");
}

遍历数组示例:

fn main() {
    test1();
}
fn test1(){
    let a=[1,2,3,4,5];
    let mut index=0;
    while index<5{
        println!("the value of a[{index}] is {} ",a[index]);
        index+=1;
    }
}

2.3 for

for的遍历array的写法很类似于cpp中的for(auto ele:array)

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

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

一个倒计时的例子

fn main() {
    for number in (1..4).rev() {
        println!("{number}!");
    }
    println!("LIFTOFF!!!");
}
//输出结果 3 2 1

标签:count,控制流,number,while,let,fn,println,main,loop
From: https://www.cnblogs.com/SaltyCheese/p/17302549.html

相关文章

  • 深入理解 python 虚拟机:字节码教程(2)——控制流是如何实现的?
    深入理解python虚拟机:字节码教程(2)——控制流是如何实现的?在本篇文章当中主要给大家分析python当中与控制流有关的字节码,通过对这部分字节码的了解,我们可以更加深入了解python字节码的执行过程和控制流实现原理。控制流实现控制流这部分代码主要涉及下面几条字节码指令,......
  • 尝试Questa仿真报错:Error while trying to run Questa simulator
        最近在看一些芯片验证的书籍,逐步学习数字芯片的一些测试原理。以前的混合芯片测试,大多不需要了解其内部的具体原理,很多情况下,了解基本的I/O结构和通讯方式即可。但想更进一步学习,无论如何都避不开verilog了。从SSI,MSI,LSI,VLSI到ULSI,再到如今的Soc,数字电路规模的进步速......
  • Linux(CentOS7) c语言编程, 多线程入栈出栈,错误:expected ‘while’ before ‘int’
    在Centos7里,编写多线程的入栈出栈时,出现这样错误提示:图片版: 文字版:[root@CentOs705-xitongbiancheng]#gcc05-24-01.pthread-cancel-pop-push.c-pthread05-24-01.pthread-cancel-pop-push.c:在函数‘func’中:05-24-01.pthread-cancel-pop-push.c:47:1:错误:expected......
  • <npm > pm ERR! code ERESOLVE npm ERR! ERESOLVE could not resolve npm ERR! npm E
    报错内容npmielement-ui-SnpmERR!codeERESOLVEnpmERR!ERESOLVEcouldnotresolvenpmERR!npmERR!Whileresolving:undefined@undefinednpmERR!Found:[email protected]!node_modules/vuenpmERR!peervue@"3.2.47"from@vue/server-rende......
  • g_main_loop 基础用法
    /*test.c*/intmain(intargc,charconst*argv[]){/*1.创建一个GMainLoop结构体对象,作为一个主事件循环*/GMainLoop*loop=g_main_loop_new(NULL,FALSE);/*2.添加超时事件源*/g_timeout_add(1000,count_down,NULL);g_timeout_add(80......
  • Rust 的函数以及 if 控制流
    楔子本篇文章来说一说Rust的函数和流程控制,首先Rust使用蛇形命名法(snakecase)来作为函数和变量的命名风格,蛇形命名法只使用小写的字母进行命名,并以下画线分隔单词。fnmain(){another_func();}fnanother_func(){println!("helloworld");}执行完之后屏幕......
  • 读取数据库返回 ResultSet的时候,遍历数据用while (rs.next())数据量大的时候很慢。
    在做中台数据的时候,需要同步其他部位的系统的数据,目前同步数据要和其他部位的数据库对接,对方数据库为Oracle在读取的过程中,数据量比较大,已经做了分页处理,分页大小为1000行......
  • error while loading shared libraries: libxxx.so: cannot open shared object file:
    发生这种问题就在于编译程序链接的库在运行时找不到,或者不存在,或者版本不正确等。使用ldd你的应用程序|grep-i"libxxx"来查看程序中具体链接的库的位置和版本号,然后查看......
  • python中的while True
    建立一个用户登录系统,用户输入用户名和密码,如果正确就可以进入系统。1、我自己最开始的写法:1d={}#数据库字典,所有用户的用户名密码存储在此25name=......
  • [bx] 和 Loop指令
    在masm编译器中不同于debug的命令如:在debug中movax,[0]-->是说将偏移地址为0中的数据送入ax中而在汇编语言中movax,[0]-->是说将0这个数据送入ax中[bx]表......