To rotate items in a vector in Rust, you can use the `rotate_left` and `rotate_right` methods provided by the standard library.
In this example,
fn main() {
// Define a vector containing elements
let mut vector = vec![1, 2, 3, 4, 5];
// Specify the number of positions to rotate the items to the left
let rotate_by = 2;
// Rotate the items to the left by the specified number of positions
vector.rotate_left(rotate_by);
// Print the rotated vector
println!("{:?}", vector);
}
[3, 4, 5, 1, 2]
In this example,
fn main() {
// Define a vector containing elements
let mut vector = vec![1, 2, 3, 4, 5];
// Specify the number of positions to rotate the items to the right
let rotate_by = 2;
// Rotate the items to the right by the specified number of positions
vector.rotate_right(rotate_by);
// Print the rotated vector
println!("{:?}", vector);
}
[4, 5, 1, 2, 3]
In this tutorial, we learned How to Rotate Items in a Vector in Rust language with well detailed examples.