0

Similar to How to match struct fields in Rust?, is it possible to match a struct like Default without physically writing out the fields? I do not want to write out the fields constantly.

Something along the lines of:

let someValue = Struct { /* ... */ };
match someValue {
    Struct::default() => println!("Default!"),
    _ => println!("Not Default"),
}

This gives an error.

I did some testing on the Rust Playground but I only ended up running into the problem of matching named variables described in the docs.

What is your best solution to comparing many structs? Is it using #[derive(PartialEq)] and if statements?

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
BadCoder
  • 141
  • 1
  • 16
  • *Is it using `#[derive(PartialEq)]`* — why *wouldn't* you do this? – Shepmaster May 12 '20 at 18:42
  • That is my question Shepmaster. I want to know if it is possible to match Structs or if PartialEq is the best/only option. – BadCoder May 12 '20 at 18:46
  • But you haven't stated what the problem with `PartialEq` is. – Shepmaster May 12 '20 at 18:48
  • Your question might be answered by the answers of [Why is this match pattern unreachable when using non-literal patterns?](https://stackoverflow.com/a/28226030/155423). If so, we can mark this question as already answered. – Shepmaster May 12 '20 at 18:49
  • That was the matching named variable problem I mentioned in my post. I tried to solve this by attempting to dynamically place it in match. This is shown in the Rust Playground code I made. That gave errors, but if written out it works. My question is not only to figure out if its possible to have a work around, but if there are other solutions people have found that are good apart from simply using PartialEq in a if statement chain. – BadCoder May 12 '20 at 18:58
  • It's hard to answer multiple questions made in one post. Please separate them into multiple questions so that we can help you better and so that your questions will help others in the future that have one of the same questions as you! – Shepmaster May 12 '20 at 19:02
  • You asked "is it possible to match a struct like Default without physically writing out the fields?" and the answer is shown in the previously linked question. – Shepmaster May 12 '20 at 19:03
  • Yes it did solve one of my question. If required I could edit my question or make a new post. – BadCoder May 12 '20 at 19:05
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/213722/discussion-between-badcoder-and-shepmaster). – BadCoder May 12 '20 at 19:16
  • 2
    Does this answer your question? [Why is this match pattern unreachable when using non-literal patterns?](https://stackoverflow.com/questions/28225958/why-is-this-match-pattern-unreachable-when-using-non-literal-patterns) – E_net4 Jun 10 '20 at 22:12

1 Answers1

1

Rust's patterns aren't values to compare to. They're more related to variable assignment (destructuring).

There is a "match guard" syntax that can be used:

match some_value {
   tmp if tmp == Struct::default() => /* it's default-like-ish */
}
Kornel
  • 97,764
  • 37
  • 219
  • 309