rust/src/liballoc
Corey Farwell e2e8cd3d14 Rollup merge of #46777 - frewsxcv:frewsxcv-rotate, r=alexcrichton
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]: https://github.com/rust-lang/rust/issues/41891
2018-01-09 22:28:23 -05:00
..
benches Deprecate [T]::rotate in favor of [T]::rotate_{left,right}. 2017-12-24 23:01:24 -08:00
btree Write examples for {BTree,Hash}Set::{get,replace,take} 2018-01-05 15:02:10 +01:00
tests Rollup merge of #46777 - frewsxcv:frewsxcv-rotate, r=alexcrichton 2018-01-09 22:28:23 -05:00
allocator.rs Use Try syntax for Option in place of macros or match 2017-12-09 14:18:33 -08:00
arc.rs Remove transmute in From<&str> impls for Arc/Rc 2017-12-25 17:04:45 -05:00
binary_heap.rs address some FIXMEs whose associated issues were marked as closed 2017-09-30 11:33:47 +03:00
borrow.rs examples in Cow::into_owned don't need to wrap result in Cows 2017-11-14 18:23:24 -06:00
boxed.rs Update bootstrap compiler 2017-11-29 21:11:20 -08:00
boxed_test.rs Direct conversions between slices and boxes. 2017-02-06 18:53:13 -05:00
Cargo.toml std: Remove rand crate and module 2017-11-08 20:41:17 -08:00
fmt.rs Adding eprint*! to the list of macros in the format! family 2017-11-22 20:44:05 -08:00
heap.rs std: Mark allocation functions as nounwind 2017-08-28 08:06:52 -07:00
lib.rs std: Remove rand crate and module 2017-11-08 20:41:17 -08:00
linked_list.rs Revert "Make drop impl stable for DrainFilter" 2017-12-09 01:09:23 -07:00
macros.rs fix some typos 2017-11-21 15:33:45 +01:00
range.rs Revert "Stabilize RangeArgument" 2017-06-30 08:34:53 -10:00
raw_vec.rs Fix typo. 2017-11-01 21:02:08 +08:00
rc.rs Remove transmute in From<&str> impls for Arc/Rc 2017-12-25 17:04:45 -05:00
slice.rs Rollup merge of #46777 - frewsxcv:frewsxcv-rotate, r=alexcrichton 2018-01-09 22:28:23 -05:00
str.rs update char_indices example to highlight big chars 2017-12-27 16:27:57 -06:00
string.rs Use Try syntax for Option in place of macros or match 2017-12-09 14:18:33 -08:00
vec.rs Fix panic condition docs for Vec::insert. 2018-01-01 19:06:59 -08:00
vec_deque.rs address some FIXMEs whose associated issues were marked as closed 2017-09-30 11:33:47 +03:00