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

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

3. 课后作业

3.1 摄氏度和华氏度的转换

fn main() {
    //test1
    let c=23.0;
    let f=temperature_convert(c,true);
    println!("c={c},f={f}");
    let cc=temperature_convert(f,false);
    println!("f={f},cc={cc}");
}
fn temperature_convert(temperature:f64,c2f:bool)->f64{
    // c->f
    if c2f {
        32.0+temperature*1.8
    }else{
        (temperature-32.0)/1.8
    }
}

3.2 生成n阶斐波那契数列

这边以10阶为例

fn main() {
    //test2
    let mut a=1;
    let mut b=1;
    let mut i=0;
    while i<10{
        println!("{a}");
        let temp=b;
        b=a+b;
        a=temp;
        i+=1;
    }
}

3.3 圣诞颂歌

一个比较简单的实现方式:

fn main() {
    //test3
    let array=[
        "partridge in a pear tree",
        "Two turtle doves",
        "Three french hens",
        "Four calling birds",
        "Five golden rings",
        "Six geese a laying",
        "Seven swans are swimming",
        "Eight maids are milking",
        "Nine ladies dancing",
        "Ten lords are leaping",
        "Eleven pipers piping",
        "Twelve drummers drumming"
    ];
    let days=[
        "first",
        "second",
        "third",
        "forth",
        "fifth",
        "sixth",
        "seventh",
        "eighth",
        "nineth",
        "tenth",
        "eleventh",
        "twelfth"
    ];
    let mut day=1;
    while day<=12{
        println!("On the {} day of Christmas",days[day-1]);
        println!("My true love sent to me");
        let mut cnt=day;
        while cnt >0 {
            cnt-=1;
            if cnt==0 && day==1{
                println!("A {}",array[cnt]);
            }else if cnt ==0{
                println!("And a {}",array[cnt]);
            }else{
                println!("{}",array[cnt]);
            }
            if cnt<=0{
                break;
            }
        }
        println!("=================================");
        day+=1;
    }
}

输出为:

On the first day of Christmas
My true love sent to me
A partridge in a pear tree
=================================
On the second day of Christmas
My true love sent to me
Two turtle doves
And a partridge in a pear tree
=================================
On the third day of Christmas
My true love sent to me
Three french hens
Two turtle doves
And a partridge in a pear tree
=================================
On the forth day of Christmas
My true love sent to me
Four calling birds
Three french hens
Two turtle doves
And a partridge in a pear tree
=================================
On the fifth day of Christmas
My true love sent to me
Five golden rings
Four calling birds
Three french hens
Two turtle doves
And a partridge in a pear tree
=================================
On the sixth day of Christmas
My true love sent to me
Six geese a laying
Five golden rings
Four calling birds
Three french hens
Two turtle doves
And a partridge in a pear tree
=================================
On the seventh day of Christmas
My true love sent to me
Seven swans are swimming
Six geese a laying
Five golden rings
Four calling birds
Three french hens
Two turtle doves
And a partridge in a pear tree
=================================
On the eighth day of Christmas
My true love sent to me
Eight maids are milking
Seven swans are swimming
Six geese a laying
Five golden rings
Four calling birds
Three french hens
Two turtle doves
And a partridge in a pear tree
=================================
On the nineth day of Christmas
My true love sent to me
Nine ladies dancing
Eight maids are milking
Seven swans are swimming
Six geese a laying
Five golden rings
Four calling birds
Three french hens
Two turtle doves
And a partridge in a pear tree
=================================
On the tenth day of Christmas
My true love sent to me
Ten lords are leaping
Nine ladies dancing
Eight maids are milking
Seven swans are swimming
Six geese a laying
Five golden rings
Four calling birds
Three french hens
Two turtle doves
And a partridge in a pear tree
=================================
On the eleventh day of Christmas
My true love sent to me
Eleven pipers piping
Ten lords are leaping
Nine ladies dancing
Eight maids are milking
Seven swans are swimming
Six geese a laying
Five golden rings
Four calling birds
Three french hens
Two turtle doves
And a partridge in a pear tree
=================================
On the twelfth day of Christmas
My true love sent to me
Twelve drummers drumming
Eleven pipers piping
Ten lords are leaping
Nine ladies dancing
Eight maids are milking
Seven swans are swimming
Six geese a laying
Five golden rings
Four calling birds
Three french hens
Two turtle doves
And a partridge in a pear tree
=================================
posted @ 2023-04-11 09:39  理想国的糕  阅读(19)  评论(0编辑  收藏  举报