0

I am trying to compare a given input to a function against several fields of a struct

struct Bla {
    bla1: u32,
    bla2: u32,
}

impl Bla {
    fn new() -> Self {
        Self { 
            bla1: 5,
            bla2: 6,
        }
    }
    
    fn check(&self, b: u32) -> bool {
        match b {
            self.bla1 => true,
            self.bla2 => true,
            _ => false
        }
    }
}


fn main() {
    let bli: Bla = Bla::new();
    
    println!("{}", bli.check(5));
}

However, I get a compile error

   Compiling playground v0.0.1 (/playground)
error: expected one of `!`, `(`, `...`, `..=`, `..`, `::`, `=>`, `if`, `{`, or `|`, found `.`
  --> src/main.rs:16:17
   |
16 |             self.bla1 => true,
   |                 ^ expected one of 10 possible tokens

error: could not compile `playground` due to previous error

Why can't I perform the match I tried to do?

Joshhh
  • 425
  • 1
  • 4
  • 18
  • 1
    The `match` statement expects a pattern. Just use if/else – Svetlin Zarev Sep 15 '21 at 13:44
  • `fn check(&self, b: u32) -> bool { self.bla1 == b || self.bla2 == b }`. – eggyal Sep 15 '21 at 15:14
  • The expression in the pattern position of a match arm has to more or less be a literal expression or one of a handful of simple compositions of literal expressions. You can't match arbitrary expressions for equality. https://doc.rust-lang.org/reference/patterns.html – BallpointBen Sep 15 '21 at 16:44

0 Answers0