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.
Currently if you want to rotate a VecDeque n elements to the left or right, you'd have to write something like this:
I wonder if it would be possible to add 2 functions:
You then only have to write:
This would be equal to slice's
rotate_leftandrotate_right.It could probably be implemented on a lower level than with pop's and pushes for a little more efficiency.