rust 条件语句和循环的用法 -7

目录

 

一,条件语句

二,循环

(一)loop,while , for 三种循环 ,三个返回关键字 continue , break , return

(二)for动态修改数组的值

(三)得到数组的下标

(四)跳出多重循环


 

一,条件语句

   (一) 四种条件语句的基础用法用法


    //1.第一种用法  if  else
    let flag = true;
    if flag {
        println!("true");
    } else {
        println!("false")
    }

    //2.第二种用法  if  else if  else
    let num = 3;
    if  num==1  {
        println!("1");
    } else if flag && num ==2  {
        println!("2");
    }  else {
        println!("3");
    }

    //3.第三种用法 if可以具有返回值,多个分支中的返回值类型必须一致
    let num  = if flag {
        1  //后面不允许跟;
    } else {
        2
    };
    println!("{:?}",num);


    //4.rust管这叫匹配,不过我真心觉得这货是swith
    let x = 5;
    match x {
        1 => println!("one"),
        2 => println!("two"),
        3 => println!("three"),
        4 => println!("four"),
        5 => println!("five"),
        _ => println!("something else")//默认是必须的
    }
    

(二)if let  和 while let 条件语句

    // if let 表达式的缺点在于其穷尽性没有为编译器所检查,而 match 表达式则检查了。
    // 如果去掉最后的 else 块而遗漏处理一些情况,编译器也不会警告这类可能的逻辑错误。
    let mut num ;
    let color: Option<&str> = None;
    if let Some(cl) = color {
        num =1;
    } else if 1==1 {
        num =2;
    }  else {
        num =3;
    }

    //返回值不加 ;
    let bool1 = true;
    let num = if bool1 {
        6
    } else {
        7
    };


    // while let 条件循环
    let mut list = Vec::new();

    list.push(1);
    list.push(2);

    while let Some(top) = list.pop() {
        println!("{}", top);
    }

二,循环

  (一)loop,while , for 三种循环 ,三个返回关键字 continue , break , return

fn main() {
    loop_f();//可以用死循环 loop
    while_f();//可以用死循环 while (true)
    for_f();
}

fn loop_f(){
    let mut num = 1;
    let m = loop {
        num +=1;
        if num == 2 {
            println!("loop_f num= {}",num);
            continue;
        }else  if num == 3 {
            break num*10;
        }
    };
    println!("loop_f num= {} , m = {}",num, m);
}

fn while_f(){
    let mut num = 1;
    while num!=2 {
        num+=1;
        println!("while_f num = {}",num)
    }
}

fn for_f(){
    let array = [6,7,8];
    let mut i = 0;
   //.iter()// 正循环   .rev() //反循环
    for num in array.iter() {
        i+=1;
        if i == 2 {
            return;
        }
        println!("for_f num = {}", num );
    }
    //或者这样,给出一个含有这之间值得迭代器。当然它不包括上限值array.len(), 0,1...array.len()-1
    for num in 0..array.len() {
        if num == 2 {
            return;
        }
        println!("for_f num = {}", num );
    }
}

(二)for动态修改数组的值

    let mut array = [6,7,8];
    for num in 0..array.len() {
        array[num] =  array[num]+1;
    }
    println!("for_f num = {:?}", array );


console:

for_f num = [7, 8, 9]

(三)得到数组的下标

//我们需要知道当前元素的下标,这样显然比较费劲,rust 提供了enumerate 函数
let mut array = [6,7,8];
let mut j = 0;
for i in array.iter(){
    println!("i = {} and j = {}", i, j);
    j=j+1;
}

println!("array  enumerate");
for (i, &j) in array.iter().enumerate() {
    println!("i = {} and j = {}", i, j);
}

println!("()  enumerate");
for (i,j) in (5..7).enumerate() {
    println!("i = {} and j = {}", i, j);
}

console:

i = 6 and j = 0
i = 7 and j = 1
i = 8 and j = 2
array  enumerate
i = 0 and j = 6
i = 1 and j = 7
i = 2 and j = 8
()  enumerate
i = 0 and j = 5
i = 1 and j = 6

(四)跳出多重循环

逻辑和使用循环标签(Loop labels)

使用循环标签 continue  相当于 break, break相当于 return。不过标签在特殊情况下更灵活

fn one(){
    'outer: for x in 0..4 {
                println!(" one x: {}",x);
                for y in 0..4 {
                    if x==2 && y==2  {
                       println!(" one x: {}, y: {}", x, y);
                       continue 'outer;
                    }
                }
            }
}

fn two(){
    for x in 0..4 {
        println!(" two  x: {}",x);
        for y in 0..4 {
            if x==2 && y==2  {
                println!(" two  x: {}, y: {}", x, y);
                break ;
            }
        }
    }
}

fn three(){
    for x in 0..4 {
        println!(" three   x: {}",x);
        for y in 0..4 {
            if x==2 && y==2  {
                println!(" three   x: {}, y: {}", x, y);
                return ;
            }
        }
    }
}

fn four(){
    'outer: for x in 0..4 {
                println!(" four x: {}",x);
                for y in 0..4 {
                    if x==2 && y==2  {
                       println!(" four x: {}, y: {}", x, y);
                       break 'outer;
                    }
                }
            }
}

console:

 one x: 0
 one x: 1
 one x: 2
 one x: 2, y: 2
 one x: 3
 two  x: 0
 two  x: 1
 two  x: 2
 two  x: 2, y: 2
 two  x: 3
 three   x: 0
 three   x: 1
 three   x: 2
 three   x: 2, y: 2
 four x: 0
 four x: 1
 four x: 2
 four x: 2, y: 2

猜你喜欢

转载自blog.csdn.net/qq_39308071/article/details/113116033