Skip to content

Should be able to take an immutable slice of a mutable array #17293

Closed
@nrc

Description

@nrc

e.g., this should work:

    let x: &mut [int] = &mut [1, 2, 3, 4, 5];
    let y = x[1..2];

Currently, it does not find the Slice trait for &mut [T]. That is because both the Slice and SliceMut traits are implemented for [T]. SliceMut methods take self as &mut self whereas Slice methods are & self. We do no coercions when looking up methods for overloaded operators, so we don't do the &mut [T] -> &[T] coercion we would do when doing a normal trait method search.

We have three options:
1 leave as is, this seems surprising and unergonomic;
2 do a special &mut -> & coercion when searching for slice methods. I suspect we want something similar for Index etc. when we implement those in a DST-ish way;
3 allow proper coercions when desugaring Index, Slice, etc. - this is potentially complicated.

I prefer option 3, but I have not thought through the repercussions.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions