Just get a boolean result from "if let"?

Viewed 95

Is there a more concise way to write this?

   let neg = if let Some(b'-') = self.peek() {
        true
   } else {
        false
   };

Where peek() returns an Option<u8>, if it wasn't clear from the code above.

2 Answers

The other answer is accurate for the specific example because of Eq, but I'll answer in the general case.

Rust now has a matches! macro for this usecase, so you can also write

let neg = matches!(self.peek(), Some(b'-'));

if you don't want to or are unable to use Eq.

Related