Suppose I have some struct Foo
like so:
struct Foo {
bar: String,
baz: u32,
... // There could be more fields, but it isn't really relevant.
}
Is there a way to construct a struct pattern that affirms Foo.bar
is some known String? For example, if method calls were allowed in patterns, something like this would solve the problem:
let qux: Foo = Foo::randomized(); // irrelevant!
match qux {
Foo { bar: "quux".to_owned(), baz, ..} => println!("qux.bar was \"quux\"!"),
_ => panic!("qux.bar was not \"quux\"!")
}
I am aware of the following options:
- Extract the fields to a tuple beforehand, converting to &str while doing so, and matching against the tuple.
- Using match guards and calling methods there.
The goal here is more to understand the way patterns work with structs than to solve any specific problem. An example of a desired answer I suspect might exist is:
There is some trait or set of traits that can be implemented for
Foo
that allow you to match against&qux
, where&qux
treatsqux.bar
as&str
rather than&String
.