-
Notifications
You must be signed in to change notification settings - Fork 13k
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
Deprecate [T]::rotate in favor of [T]::rotate_{left,right}. #46777
Conversation
r? @BurntSushi (rust_highfive has picked a reviewer for you, use r? to override) |
If this needs to go through an RFC, let me know and I can throw one together 💁♂️ Also cc @scottmcm since you wrote the initial |
0f58d20
to
9350d56
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems good to me. The left/right is probably better since the takes a number (as opposed to some "position" concept, where rotating the two sides without mentioning a direction is the better API).
/// | ||
/// Takes linear (in `self.len()`) time. | ||
/// | ||
/// # Examples |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider including an example that rotates a subslice, since that seems to be such a common confusion (see the recent changes to copy_from_slice and such).
src/liballoc/slice.rs
Outdated
/// ``` | ||
#[unstable(feature = "slice_rotate", issue = "41891")] | ||
pub fn rotate(&mut self, mid: usize) { | ||
core_slice::SliceExt::rotate(self, mid); | ||
pub fn rotate_right(&mut self, mid: usize) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One more thought here: rotate
(and thus rotate_left
) used mid
for the parameter because it has a useful interpretation as an index, the same as split_at
(which uses the same parameter name).
rotate_right
, however, doesn't have that same meaning, so it should have a different name. I propose k
, the letter used in EoP (the book I was following while trying various possible implementations) and common in a quick search of specifically to-the-right rotation problems online.
9350d56
to
80a9ac5
Compare
@scottmcm thanks for the feedback! both your comments have been addressed in the latest force push |
Error looks non-transient, though weird: [00:02:45] Compiling alloc v0.0.0 (file:///checkout/src/liballoc)
[00:02:45] error: missing `fn`, `type`, or `const` for impl-item declaration
[00:02:45] --> /checkout/src/liballoc/slice.rs:1437:33
[00:02:45] |
[00:02:45] 1437 | /// a[1..5].rotate_right(1);
[00:02:45] | _________________________________^
[00:02:45] 1438 | | l /// assert_eq!(a, ['a', 'e', 'b', 'c', 'd', 'f']);
[00:02:45] | | missing `fn`, `type`, or `const`
[00:02:45]
[00:02:48] error: aborting due to previous error
[00:02:48]
[00:02:48] error: Could not compile `alloc`. https://github.com/rust-lang/rust/pull/46777/files#diff-773807740b9d7f176c85b4e2e34b2607R1437 |
src/liballoc/slice.rs
Outdated
/// ``` | ||
/// let mut a = ['a', 'b', 'c', 'd', 'e', 'f']; | ||
/// a[1..5].rotate_right(1); | ||
l /// assert_eq!(a, ['a', 'e', 'b', 'c', 'd', 'f']); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's an l
at the start of the line 😄
Also, the code example is missing #![feature(slice_rotate)]
.
80a9ac5
to
9cf9262
Compare
ci errors fixed in the latest force push |
src/libcore/slice/mod.rs
Outdated
@@ -650,6 +653,16 @@ impl<T> SliceExt for [T] { | |||
} | |||
} | |||
|
|||
fn rotate_right(&mut self, mid: usize) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: k
here too.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
good call! addressed in the latest force push
9cf9262
to
40d2003
Compare
Background ========== Slices currently have an unstable [`rotate`] method which rotates elements in the slice to the _left_ N positions. [Here][tracking] is the tracking issue for this unstable feature. ```rust let mut a = ['a', 'b' ,'c', 'd', 'e', 'f']; a.rotate(2); assert_eq!(a, ['c', 'd', 'e', 'f', 'a', 'b']); ``` Proposal ======== Deprecate the [`rotate`] method and introduce `rotate_left` and `rotate_right` methods. ```rust let mut a = ['a', 'b' ,'c', 'd', 'e', 'f']; a.rotate_left(2); assert_eq!(a, ['c', 'd', 'e', 'f', 'a', 'b']); ``` ```rust let mut a = ['a', 'b' ,'c', 'd', 'e', 'f']; a.rotate_right(2); assert_eq!(a, ['e', 'f', 'a', 'b', 'c', 'd']); ``` Justification ============= I used this method today for my first time and (probably because I’m a naive westerner who reads LTR) was surprised when the docs mentioned that elements get rotated in a left-ward direction. I was in a situation where I needed to shift elements in a right-ward direction and had to context switch from the main problem I was working on and think how much to rotate left in order to accomplish the right-ward rotation I needed. Ruby’s `Array.rotate` shifts left-ward, Python’s `deque.rotate` shifts right-ward. Both of their implementations allow passing negative numbers to shift in the opposite direction respectively. Introducing `rotate_left` and `rotate_right` would: - remove ambiguity about direction (alleviating need to read docs 😉) - make it easier for people who need to rotate right [`rotate`]: https://doc.rust-lang.org/std/primitive.slice.html#method.rotate [tracking]: rust-lang#41891
40d2003
to
66ef6b9
Compare
Are "left" and "right" the correct terms to use here or does this suffer from the same issue as |
Given that slices can be trivially represented as |
|
Does 'forward' mean 'towards the back' or 'towards the front'? I could see arguments for either |
Okay, I'm embarrassed now since apparently the definition of 'forward' is literally "toward the front" in the dictionary. I guess that would means 'forward' is rotate-left? |
@frewsxcv FWIW, I also was uncertain, perhaps since "iterating forward" is starts at the front and moves toward the back... |
anyone from @rust-lang/libs have thoughts about this? |
Regarding alternate names, there was previous discussion in #41891. |
📌 Commit 66ef6b9 has been approved by |
@bors rollup |
…chton Deprecate [T]::rotate in favor of [T]::rotate_{left,right}. Background ========== Slices currently have an **unstable** [`rotate`] method which rotates elements in the slice to the _left_ N positions. [Here][tracking] is the tracking issue for this unstable feature. ```rust let mut a = ['a', 'b' ,'c', 'd', 'e', 'f']; a.rotate(2); assert_eq!(a, ['c', 'd', 'e', 'f', 'a', 'b']); ``` Proposal ======== Deprecate the [`rotate`] method and introduce `rotate_left` and `rotate_right` methods. ```rust let mut a = ['a', 'b' ,'c', 'd', 'e', 'f']; a.rotate_left(2); assert_eq!(a, ['c', 'd', 'e', 'f', 'a', 'b']); ``` ```rust let mut a = ['a', 'b' ,'c', 'd', 'e', 'f']; a.rotate_right(2); assert_eq!(a, ['e', 'f', 'a', 'b', 'c', 'd']); ``` Justification ============= I used this method today for my first time and (probably because I’m a naive westerner who reads LTR) was surprised when the docs mentioned that elements get rotated in a left-ward direction. I was in a situation where I needed to shift elements in a right-ward direction and had to context switch from the main problem I was working on and think how much to rotate left in order to accomplish the right-ward rotation I needed. Ruby’s `Array.rotate` shifts left-ward, Python’s `deque.rotate` shifts right-ward. Both of their implementations allow passing negative numbers to shift in the opposite direction respectively. The current `rotate` implementation takes an unsigned integer argument which doesn't allow the negative number behavior. Introducing `rotate_left` and `rotate_right` would: - remove ambiguity about direction (alleviating need to read docs 😉) - make it easier for people who need to rotate right [`rotate`]: https://doc.rust-lang.org/std/primitive.slice.html#method.rotate [tracking]: rust-lang#41891
Just a note, the docs still say rotate. |
the new methods haven't hit stable or beta yet, so youll need a nightly compiler and be looking at the nightly docs https://doc.rust-lang.org/nightly/std/primitive.slice.html#method.rotate_left |
Ah sorry, didn't notice that. I'd assumed that since the stable docs had nightly methods they'd be up to date with them. |
Stable docs do render unstable methods, but these new methods need to ride the release train from nightly to beta and stable before the methods show up in beta and stable docs |
Ah yes this makes sense, I forget that the thing blocking nightly features from stable is specifically feature gates. Thanks :) |
The `rotate` method has been deprecated in favor of `rotate_left` and `rotate_right` since rust-lang/rust#46777, and will be deleted in rust-lang/rust#48450.
The `rotate` method has been deprecated in favor of `rotate_left` and `rotate_right` since rust-lang/rust#46777, and will be deleted in rust-lang/rust#48450.
Background
Slices currently have an unstable
rotate
method which rotateselements in the slice to the left N positions. Here is the
tracking issue for this unstable feature.
Proposal
Deprecate the
rotate
method and introducerotate_left
androtate_right
methods.Justification
I used this method today for my first time and (probably because I’m a
naive westerner who reads LTR) was surprised when the docs mentioned that
elements get rotated in a left-ward direction. I was in a situation
where I needed to shift elements in a right-ward direction and had to
context switch from the main problem I was working on and think how much
to rotate left in order to accomplish the right-ward rotation I needed.
Ruby’s
Array.rotate
shifts left-ward, Python’sdeque.rotate
shiftsright-ward. Both of their implementations allow passing negative numbers
to shift in the opposite direction respectively. The current
rotate
implementation takes an unsigned integer argument which doesn't allow
the negative number behavior.
Introducing
rotate_left
androtate_right
would: