Closed
Description
This is a sub-issue of the larger tracking issue for slice patterns (#23121). It specifically tracks the plans to stabilize fixed-length slice patterns -- that is, excluding the ..
operator (which is entangled with some syntactic questions). There is an implementation PR in #48516.
Summary
Using this feature, a pattern like [a, b, c]
can be used to match against fixed-length arrays:
let arr = [1, 2, 3];
let [a, b, c] = arr; // where arr: [T;3]
let [a, ref b, c] = arr; // where arr: [T;3]
One can also use such a pattern to match against slices:
let arr = &[1, 2];
match arr {
[a, b, c] => { /* this arm will not apply */ }
[a, b] => { /* this arm will */ }
_ => { /* this wildcard is required, since we don't know length statically */
}
Notable points and tests
Here are some of the things I think we should be testing. I didn't have time to find if we have all these tests, perhaps someone can help (cc @petrochenkov, @mikhail-m1 ?)
- test showing irrefutable matches (
let [a, b] =
) applied to fixed-length to move things out - test showing irrefutable matches (
let [a, b] =
) applied to create reference - test showing attempt to move from a borrowed array or slice gives error
- move-out-of-slice1.rs sort of tests that, but for an oddly special case involving box patterns
- test that wildcard is required when matching slice with fixed-length arrays
match-vec-fixed
tests that we get warnings for two slice patterns of same length- other cases?
Things not stabilized
- The main thing not stabilized is the ability to create a slice of unmatched items.