Skip to content

Suggest PartialEq bound for binary operation == error #73416

Closed
@tesuji

Description

@tesuji

Try this snippet: playground

pub fn strip_prefix<'a, T>(s: &'a [T], prefix: &[T]) -> Option<&'a [T]>
// where 
//     T: PartialEq
{
    let n = prefix.len();
    if n <= s.len() {
        let (head, tail) = s.split_at(n);
        if head == prefix {
            return Some(tail);
        }
    }
    None
}

The compiler errors out:

error[E0369]: binary operation `==` cannot be applied to type `&[T]`
 --> src/lib.rs:8:17
  |
8 |         if head == prefix {
  |            ---- ^^ ------ &[T]
  |            |
  |            &[T]

error: aborting due to previous error

For more information about this error, try `rustc --explain E0369`.

It would be nice if the compiler could suggest adding PartialEq bound for T.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`C-enhancementCategory: An issue proposing an enhancement or a PR with one.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