0

My very first rust question. Searched large and far but haven't found the answer yet.

pub fn open(&mut self, row: usize, col: usize) {
    let n = self.n; // first question, I had to use this, I couldn't just use self.n as matching expression...
                    // ... a bit more stuff here
    match row {
        1 => {
            do_up = false;
            self.union(self.cell(row, col), 0)
        }
        n => {
            println!("this");
            do_down = false;
            self.union(self.cell(row, col), self.n)
        }
        _ => {}
    }
}

In my app, row can have different values from 1 to n. The result I am getting is that all other rows than 1 enter the n option.

What I want is that the option 1 code only runs when row is indeed 1, and the n option only when it's indeed n (e.g. if n is 5, then only match row == 5), and all the other values for row just get skipped.

Also, bonus question, if I put self.n directly as an option, then rustc complains. I had to introduce let n = self.n for it to compile. My intuition tells me that the actual reason why it's not working as intended is related to just that...

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
transient_loop
  • 5,984
  • 15
  • 58
  • 117
  • 1
    Can you narrow this to one _specific_ technical question, summarized in the title? Putting multiple questions in a single SO question makes it eligible as close as "too broad", and a title should help others with the same problem identify clearly whether a questions answers will help them; "don't get" can mean different things. – Charles Duffy Mar 08 '23 at 15:52
  • ...I've tried to edit the title to be more specific myself; feedback on whether that's an improvement would be welcome. – Charles Duffy Mar 08 '23 at 15:53
  • What’s the difference between match `n` and `_`? – Abhijit Sarkar Mar 08 '23 at 15:56
  • Yes, I guess match guards is what I need. I wasn't really sure what I was looking for. I guess in that case the question can be closed. – transient_loop Mar 08 '23 at 16:13
  • Although it's still not working for me. I have `match row {... row if row == self.n => {}}` but this never matches at all! – transient_loop Mar 08 '23 at 16:24
  • 1
    @transient_loop Please open a new question with your new code. This isn't enough code to answer your second question, and it's an independent problem. Further, please provide a [MRE], with a `main`, `use` statements and no undefined variables/types. – Finomnis Mar 08 '23 at 19:25

0 Answers0