Skip to content

Stabilize fixed-length slice patterns (excluding ..) #48836

Closed
@nikomatsakis

Description

@nikomatsakis

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
  • 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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-tracking-issueCategory: An issue tracking the progress of sth. like the implementation of an RFCT-langRelevant to the language team, which will review and decide on the PR/issue.final-comment-periodIn the final comment period and will be merged soon unless new substantive objections are raised.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions