I'm not trying to say Swift is better, by the way.
I'm not trying to say Swift is better, by the way.
- switching on tuples (not wrapped in a var): this prevents doubly-nested switches, which I've always found to be a problem in control logic. - unwrapping optionals - matching against an enum's associated value, and unwrapping it - using `where` clauses as part of the pattern - matching against type casts
Edit: I invite you to learn more about the match keyword in rust and see how it relates to swift. I think you’ll find that it meets or exceeds your expectations: https://doc.rust-lang.org/book/ch06-02-match.html
> - switching on tuples (not wrapped in a var): this prevents doubly-nested switches, which I've always found to be a problem in control logic
match t {
(Some(a), Some(b)) => a + b,
(Some(a), None) => a,
(None, Some(b)) => b,
(None, None) => 0
}
> - unwrapping optionals match opt {
Some(v) => v,
None => 0
}
although it's common to use HOFs, the `?` operator, or `if let`.> - matching against an enum's associated value, and unwrapping it
See above.
- using `where` clauses as part of the pattern
match opt {
Some(v) if v > 5 => v - 5,
Some(v) => v,
None => 0
}
> - matching against type castsRust doesn't really do subtyping, so that's not a case which would come up. I guess the closest would be matching on the result of a downcast_ref (https://doc.rust-lang.org/std/any/trait.Any.html#method.down...) but that just returns an Option so there's nothing special to it.
match e with x as (y, z) -> (x[0], x[1]) == (y, z)
This is because the matcher itself must take ownership of the expression, and destructuring is an ownership transfer.
You can bind references to what you are matching with @name. You also don't need to take ownership of a value. You can match on a reference to it with &.
However without understanding your example it is hard to say for sure.
Given what they're saying I assume they're trying to match both the entire value and the sub-values e.g.
xs@(_:xs')
in Haskell. Stable Rust currently lets you use at-patterns and perform assertions on the sub-pattern, but not match there.> You also don't need to take ownership of a value. You can match on a reference to it with &.
`ref` is what you'd use in a pattern though, `&` would deref' a reference so usually it'd be to Copy a Copy type which is provided behind a ref'.
I see. Yes, it appears to be either-or for now.
> `ref` is what you'd use in a pattern though
Not in the pattern, in the "argument". Like this: https://rust.godbolt.org/z/eT366s
pub fn f(v: &Option<String>) {
match &v {
other => eprintln!("Doesn't move {:?}", other),
}
}It doesn't, you can bind by references. It's more fiddly but nothing precludes the combination of at-patterns and sub-pattern matchings, though it's not stable yet there's a tracking issue for at-and-sub-patterns: https://github.com/rust-lang/rust/issues/65490 and it works on nightly: https://play.rust-lang.org/?version=nightly&mode=debug&editi...
There's also an issue for patterns combining by-ref and by-move bindings: https://github.com/rust-lang/rust/issues/68354
Also… does Swift even have as-patterns?
[1]: https://github.com/rust-lang/rfcs/blob/master/text/2005-matc...
[2]: https://play.rust-lang.org/?version=nightly&mode=debug&editi...