Well, I would say that the second solution is better because you don't have to explitely say true or false, but Rust's obligation to put {} for if else statements gets anoying when you have only one line inside.
I did not find anything regarding this and would be OK with both solutions. Plus, if anyone isn't happy with your match, it's a simple fix.
I would rather phrase it as "unidiomatic": Rust community overwhelmingly uses K&R style for braces. There's nothing inherently good or bad in any particular style.
For years I've had the color scheme of my editor set to show punctuation at 40% opacity. I'll reset this now so when I come across this style you quoted I'll be sure to notice it so I can get rightfully mad.
It hasn't been mentioned I don't think so I'll just say one major benefit to matching bools for a particular edge case:
Say you have combinations of bools: is_fruit = true, eaten = false, etc.
My example is trivial, but you can imagine a situation where you need to handle all possible outcomes, and the nice thing about match statements is they enforce that some behavior exists for all cases exhaustively:
match (is_fruit, eaten) {
(true, true) => "we need more fruit!".to_string()
(false, true) => "was that even fruit?".to_string()
// compiler error! Match not exhaustive!
}
So that's the only time I use match instead of if, but I'm interested to hear if there's a more idiomatic way to my suggestions.
And for those who say, just have nested ifs, well yes until the programmer forgets! match is much harder to "forget" - you'd have to forget to put an additional boolean inside the tuple, which, c'mon, that's only 1 line of code to remember...
@jhpratt that wasn't intended and i'm sorry. How do I comment without bumping? Or is the answer just don't comment at all in which case why isn't this topic just locked from further input?
I'm used to SO where answers often times extend several years as things change and new features become available
Threads here close 3 months after the last reply now, but that wasn't always the case. Just don't comment or open a new thread with a summary if you have a question.
Discourse has a pretty linear/chronological format. Posts don't shuffle by vote and are rarely edited for things other than typo corrections and the like; instead one typically makes a new comment conversation-style. If someone bumps an old thread and I don't notice, I'll read a bunch of multi-year-old comments until I catch on, like if you had to read the entire history of an SO topic first or something.
Fair, but I wasn't asking a question - I genuinely believed I was providing a valuable answer to the OP's question. I am new to Rust and mentioned I'd like to hear what others say only to assert my neophyte status with the language so my answer didn't lead to "blind leading the blind".
What I'm hearing though is old threads that don't auto-close should probably be avoided, unless I have stunning information to share which I likely won't for another 5 - 10 years once I reach expert level anyway.