0

I'm a rust newbie. This is not a homework question.

I know I can match on literals like the first match below. I've indicated where I need help below with vvvv. I'm checking if a divides b completely (if it does return a/b, else return err variant).

What pattern can I use in rust to match "not a literal"? Please suggest an idiomatic way to solve this. I was hoping to get the error variant first.

What I know:

  • I can match with 0 and use _ to match the error case.
pub fn divide(a: i32, b: i32) -> Result<i32, DivisionError> {
    match b{
        //return error variant if b is zero
        0 => DivisionError::DivideByZero,
        //if not error, then check if a divides b completely
        _ => match a%b {
          //vvvv This is where I need your help
            != 0 => 4,

        }
    }
}
Chayim Friedman
  • 47,971
  • 5
  • 48
  • 77
Dheeraj Bhaskar
  • 18,633
  • 9
  • 63
  • 66
  • 1
    Why not replace the inner `match` with just a `if a%b == 0 { } else { }`? Or if you absolutely want to use a `match`, you can do it with two cases: `0` followed by a catch-all `_`. (See the duplicate link for an answer to why `!0` doesn't work.) – gspr Feb 07 '23 at 12:40

0 Answers0