Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
nrc opened this issue Sep 16, 2014 · 2 comments
Closed

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

nrc opened this issue Sep 16, 2014 · 2 comments

Comments

@nrc
Copy link
Member

nrc commented Sep 16, 2014

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.

@nrc
Copy link
Member Author

nrc commented Dec 18, 2014

This will be fixed by the conversion of slice syntax to use range in a slice syntax

@steveklabnik
Copy link
Member

This now works (though you need &x[1..2] because we removed the coercions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants