Rust

3.5 控制流

2019-11-14  本文已影响0人  剑有偏锋

根据条件是否为真来决定是否执行某些代码,以及根据条件是否为真来重复运行一段代码是大部分编程语言的基本组成部分。Rust 代码中最常见的用来控制执行流的结构是 if 表达式和循环。

if 表达式

简单if - else

fn main() {
    let number = 3;
    if number < 5 {
        println!("condition was true");
    } else {
        println!("condition was false");
    }
}
 cargo run
   Compiling branches v0.1.0 (/home/li/projects/branches)
    Finished dev [unoptimized + debuginfo] target(s) in 0.63s
     Running `target/debug/branches`
condition was true

使用else 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");
    }
}

在let语句使用if

fn main() {
    let condition = true;
    let number = if condition {
        5
    } else {
        6
    };

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

输出

cargo run
   Compiling branches v0.1.0 (/home/li/projects/branches)
    Finished dev [unoptimized + debuginfo] target(s) in 0.38s
     Running `target/debug/branches`
The value of number is: 5

注意:代码块的值是其最后一个表达式的值,而数字本身就是一个表达式。这意味着 if 的每个分支的可能的返回值都必须是相同类型;如果它们的类型不匹配,则报错(错误示例如下: error[E0308]: if and else have incompatible types )

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

使用循环重复执行

loop

无限循环

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

带break跳出循环的loop,且使用break传递返回值

fn main() {

    let mut counter = 0;
    let result = loop {
        counter += 1;
        if counter == 10 {
           break counter * 2;
        }
    };

    assert_eq!(result, 20);
}

输出

cargo run
   Compiling branches v0.1.0 (/home/li/projects/branches)
    Finished dev [unoptimized + debuginfo] target(s) in 0.66s
     Running `target/debug/branches`

while

fn main() {

    let mut counter = 0;
    while counter != 0 {
        println!("{}!", counter);
        counter = counter -1;
    }

    println!("LIFT OFF!!!");   
}

输出

cargo run
   Compiling branches v0.1.0 (/home/li/projects/branches)
    Finished dev [unoptimized + debuginfo] target(s) in 0.40s
     Running `target/debug/branches`
LIFT OFF!!!

for

fn main() {
    let a = [10, 20, 30, 40 ,50];
    for element in a.iter() {
        println!("the value is: {}", element);
    }
}

输出

cargo run
   Compiling branches v0.1.0 (/home/li/projects/branches)
    Finished dev [unoptimized + debuginfo] target(s) in 0.44s
     Running `target/debug/branches`
the value is: 10
the value is: 20
the value is: 30
the value is: 40
the value is: 50

for 循环的安全性(对容器)和简洁性使得它成为 Rust 中使用最多的循环结构。即使是在想要循环执行代码特定次数时

使用for range

fn main() {
    for number in (1..4).rev() { //rev是逆序排列
        println!("{}!", number);
    }
    println!("LIFT OFF!");
}

输出

 cargo run
   Compiling branches v0.1.0 (/home/li/projects/branches)
    Finished dev [unoptimized + debuginfo] target(s) in 0.38s
     Running `target/debug/branches`
3!
2!
1!
LIFT OFF!
上一篇 下一篇

猜你喜欢

热点阅读