Rust学习总结之if,while,loop,for使用

 目录

一:if的使用

二:while的使用

三:loop的使用

四:for的使用


        本文总结的四种语句(if,while,loop,for)除了loop,其他的三个在C语言或者Python中都是常见并且常用的语句,在rust中增加了loop的用法,下面将详细学习一下这四种语句的使用

一:if的使用

fn main() {
    let num = 1;
    if num > 10{
        println!("number is {} large 10",num);
    }else {
        println!("number is {} less 10",num);
    }
}

运行结果:

number is 1 less 10

         上面是if条件判断语句的基本用法,if后边不需要用小括号(但是允许使用小括号,编译的时候会有告警提示),另外大括号{}是必须要的,C语言中单语句可以省略大括号,但是rust中不允许。当然在rust中if也支持if-else语法,另外if后面也可以有多个条件表达式

fn main() {
    let num = 5;

    if num > 10{
        println!("number is {} large 10",num);
    }else if num < 3 && num > 4{
        println!("number is {} less 10",num);
    }else {
        println!("number is {}",num);
    }
}

运行结果:

number is 5

另外对于简单的if判断,rust也支持一行写法,类似于python中的三元条件运算表达式

fn main() {
    let age = 18;
    let height = if age > 13 {175} else {150};
    println!("the height is {}",height);
}

运行结果:

the height is 175

 最后需要注意的是在rust中条件表达式必须是bool类型,不允许像C语言中非0就是真的写法


fn main() {
    let number = 3;
    if number {
        println!("Yes");
    }
}

运行结果:

二:while的使用

下面我们用while写一个从0循环加到100的语句

fn main() {
    let mut sum = 0;
    let mut index = 0;
    while index <= 100{
        sum += index;
        index += 1;
    }
    println!("The sum is: {}", sum);
}

运行结果:

The sum is: 5050

 上面的代码在调试的时候,假如最后一句打印没有的话,在C语言中是绝对没有问题的,但是在rust中会报sum没有使用

 目前rust的学习还不够深入,猜测可能是作用域的问题,待学习到后面再来探讨一下这个现象

当然在while循环过程中,如果想遇到某个条件要跳出while循环,也可以用break

fn main() {
    let mut sum = 0;
    let mut index = 0;
    while index <= 100{
        sum += index;
        index += 1;
        if index == 50{
            break;
        }
    }
    println!("The sum is: {}", sum);
}

运行结果:

The sum is: 1225

三:loop的使用

        loop语句在C语言中是没有的,在rust中引入了loop,给开发者多了一个选择。在C语言中假如我们要写一个循环,无法在开头和结尾判断是否继续进行循环,必须在循环体中间某处控制循环的进行。如果遇到这种情况,我们经常会在一个 while (true) 循环体里实现中途退出循环的操作。在Rust中loop就能很好的解决这个应用场景。当然loop的用法也很灵活

        还是上面的循环加到100例子,这里用loop来实现

fn main() {
    let mut sum = 0;
    let mut index = 0;
    loop{
        sum += index;
        index += 1;
        if index > 100{
            break;
        }
    }
    println!("The sum is: {}", sum);
}

运行结果:

The sum is: 5050

        loop 循环可以通过 break 关键字类似于 return 一样使整个循环退出并给予外部一个返回值。这是一个十分巧妙的设计,因为 loop 这样的循环常被用来当作查找工具使用,如果找到了某个东西当然要将这个结果交出去

fn main() {
    let s = ['f', 't', 'z', 'c', 's', 'd','n'];
    let mut i = 0;
    let location = loop {
        let ch = s[i];
        if ch == 'c' {
            break i;
        }
        i += 1;
    };
    println!(" 'c' 的索引为 {}", location);
}

运行结果:

'c' 的索引为 3

四:for的使用

        for循环可以说是一个万金油的循环结构,在C语言或者Python的开发过程中,for循环我用的非常多,当然while循环也会用,但是for循环相比while循环,一是结构上比较灵活,二是相对来说比while也安全些,特别是对于数组,可迭代结构的遍历非常方便

fn main() {
    let s = ['f', 't', 'z'];
    //遍历数组s,方法一
    for index in 0..s.len(){
        println!("遍历数组:s[{}]:{}",index,s[index]);
    }
    //遍历数组s,方法二
    for element in s.iter(){
        println!("遍历数组s:{}",element);
    }
}

运行结果:

遍历数组:s[0]:f
遍历数组:s[1]:t
遍历数组:s[2]:z
遍历数组s:f
遍历数组s:t
遍历数组s:z

可以看到rust中for循环的语法和Python非常像,看到了就有一种熟悉感。

好了!!!以上就是对rust条件判断和循环语句的学习总结,有不对的地方也欢迎批评指正,点个赞更好,嘻嘻嘻!!!

猜你喜欢

转载自blog.csdn.net/qq_27071221/article/details/129319167