Skip to content

Instantly share code, notes, and snippets.

@RandyMcMillan
Forked from rust-play/playground.rs
Last active May 14, 2025 13:11
Show Gist options
  • Save RandyMcMillan/62a52ce68d6c85071ff47768837130ef to your computer and use it in GitHub Desktop.
Save RandyMcMillan/62a52ce68d6c85071ff47768837130ef to your computer and use it in GitHub Desktop.
vec_drain.rs
fn main() {
let mut numbers = vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
println!(" 0 1 (2 3 4 5]");
println!("Original vector: {:?}", numbers);
#[allow(unused_variables)]
for num in numbers.clone() {
//print!("{} ", num);
}
//println!();
// We want to drain elements from
// index 2 (exclusive) to
// index 5 (inclusive)
let drained: Vec<_> = numbers.drain(2..5).collect();
println!("Original vector after drain: {:?}", numbers);
println!("Drained elements: {:?}", drained);
// You can also iterate over the drained elements directly
//println!("Iterating over the drained elements:");
#[allow(unused_variables)]
for num in drained.iter() {
//println!("Removed: {}", num);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment