如何用“match”重写将整数与值进行比较的“if”链?

mar*_*tyr 4 rust

fn test_if_else(c: i32) {
    if c > 0 {
        println!("The number {} is greater than zero", c);
    } else if c < 0 {
        println!("The number {} is less then zero", c);
    } else {
         println!("the number {} is equal to zero", c);
}
Run Code Online (Sandbox Code Playgroud)

这就是我身上发生的事

 match c {
    0 => println!("the number {} is equal to zero", c),
    0..infinity => println!("The number {} is greater than zero", c),
    _ => println!("the number {} is equal to zero", c)
 }
Run Code Online (Sandbox Code Playgroud)

但它不适用于“无穷大”

Net*_*ave 7

您只需要使用开放范围0..

fn test_if_else(c: i32) {
    match c {
        0 => println!("the number {} is equal to zero", c),
        0.. => println!("The number {} is greater than zero", c),
        _ => println!("the number {} is lesser than zero", c),
    }
}
Run Code Online (Sandbox Code Playgroud)

操场