Skip to content

match &[first, ..more] leads down a suggestion garden path #130639

Open

Description

Code

struct Algorithms<'a>(&'a Vec<String>);
impl<'a> fmt::Display for Algorithms<'a> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match &self.0[..] {
            &[] => f.write_str("<default>"),
            &[one] => f.write_str(&one),
            &[first, ..more] => {
                f.write_str(&first)?;
                for rec in more {
                    write!(f, ", then {}", rec)?;
                }
                Ok(())
            },
        }
    }
}

Current output

   Compiling zram-generator v1.1.2 (/home/nabijaczleweli/code/systemd-zram-generator)
error[E0425]: cannot find value `more` in this scope
   --> src/config.rs:205:24
    |
205 |             &[first, ..more] => {
    |                        ^^^^ not found in this scope
    |
help: if you meant to collect the rest of the slice in `more`, use the at operator
    |
205 |             &[first, more @ ..] => {
    |                      ~~~~~~~~~

okay so I do that and get

   Compiling zram-generator v1.1.2 (/home/nabijaczleweli/code/systemd-zram-generator)
error[E0277]: the size for values of type `[String]` cannot be known at compilation time
   --> src/config.rs:205:22
    |
205 |             &[first, more @ ..] => {
    |                      ^^^^^^^^^ doesn't have a size known at compile-time
    |
    = help: the trait `Sized` is not implemented for `[String]`
    = note: all local variables must have a statically known size
    = help: unsized locals are gated as an unstable feature

..? definitely sub-optimal; the first thing I tried was

error: `..` patterns are not allowed here
   --> src/config.rs:205:30
    |
205 |             &[first, &more @ ..] => {
    |                              ^^
    |
    = note: only allowed in tuple, tuple struct, and slice patterns

so I did &[first, ref more @ ..] which worked

Desired output

idk. probably suggest with the ref if appropriate?

Rationale and extra context

No response

Other cases

No response

Rust Version

rustc 1.80.1 (3f5fd8d 2024-08-06)
binary: rustc
commit-hash: 3f5fd8d
commit-date: 2024-08-06
host: x86_64-unknown-linux-gnu
release: 1.80.1
LLVM version: 18.1.7

Anything else?

No response

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-patternsRelating to patterns and pattern matchingA-resolveArea: Name resolutionA-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`D-invalid-suggestionDiagnostics: A structured suggestion resulting in incorrect code.D-papercutDiagnostics: An error or lint that needs small tweaks.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions