为什么创建包含范围时使用.. =而匹配包含范围时使用...?

时间:2019-06-10 20:56:11

标签: rust range

Ranges use ..= for inclusive range

for n in 1..=100 {
    if n % 15 == 0 {
        println!("fizzbuzz");
    } else if n % 3 == 0 {
        println!("fizz");
    } else if n % 5 == 0 {
        println!("buzz");
    } else {
        println!("{}", n);
    }
}

match uses ... for inclusive range时:

match number {
    // Match a single value
    1 => println!("One!"),
    // Match several values
    2 | 3 | 5 | 7 | 11 => println!("This is a prime"),
    // Match an inclusive range
    13...19 => println!("A teen"),
    // Handle the rest of cases
    _ => println!("Ain't special"),
}

0 个答案:

没有答案