-
Notifications
You must be signed in to change notification settings - Fork 13.6k
Closed
Closed
Copy link
Labels
B-unstableBlocker: Implemented in the nightly compiler and unstable.Blocker: Implemented in the nightly compiler and unstable.C-tracking-issueCategory: An issue tracking the progress of sth. like the implementation of an RFCCategory: An issue tracking the progress of sth. like the implementation of an RFCT-libs-apiRelevant to the library API team, which will review and decide on the PR/issue.Relevant to the library API team, which will review and decide on the PR/issue.disposition-mergeThis issue / PR is in PFCP or FCP with a disposition to merge it.This issue / PR is in PFCP or FCP with a disposition to merge it.finished-final-comment-periodThe final comment period is finished for this PR / Issue.The final comment period is finished for this PR / Issue.
Description
Currently if you want to rotate a VecDeque n elements to the left or right, you'd have to write something like this:
use std::collections::VecDeque;
fn main() {
let mut v = (0..10).collect::<VecDeque<_>>();
println!("{:?}", v); // [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
// rotate left by 2,
for _ in 0..2 {
if let Some(popped_front) = v.pop_front() {
v.push_back(popped_front);
}
}
println!("{:?}", v); // [2, 3, 4, 5, 6, 7, 8, 9, 0, 1]
// rotate right by 4
for _ in 0..4 {
if let Some(popped_back) = v.pop_back() {
v.push_front(popped_back);
}
}
println!("{:?}", v); //[8, 9, 0, 1, 2, 3, 4, 5, 6, 7]
}
I wonder if it would be possible to add 2 functions:
- rotate_left(mid: usize)
- rotate_right(mid: usize)
You then only have to write:
use std::collections::VecDeque;
fn main() {
let mut v = (0..10).collect::<VecDeque<_>>();
println!("{:?}", v); // [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
// rotate left by 2,
v.rotate_left(2)
println!("{:?}", v); // [2, 3, 4, 5, 6, 7, 8, 9, 0, 1]
// rotate right by 4
v.rotate_right(4);
println!("{:?}", v); //[8, 9, 0, 1, 2, 3, 4, 5, 6, 7]
}
This would be equal to slice's rotate_left
and rotate_right
.
It could probably be implemented on a lower level than with pop's and pushes for a little more efficiency.
jeffvandyke
Metadata
Metadata
Assignees
Labels
B-unstableBlocker: Implemented in the nightly compiler and unstable.Blocker: Implemented in the nightly compiler and unstable.C-tracking-issueCategory: An issue tracking the progress of sth. like the implementation of an RFCCategory: An issue tracking the progress of sth. like the implementation of an RFCT-libs-apiRelevant to the library API team, which will review and decide on the PR/issue.Relevant to the library API team, which will review and decide on the PR/issue.disposition-mergeThis issue / PR is in PFCP or FCP with a disposition to merge it.This issue / PR is in PFCP or FCP with a disposition to merge it.finished-final-comment-periodThe final comment period is finished for this PR / Issue.The final comment period is finished for this PR / Issue.