-
-
Save RandyMcMillan/62a52ce68d6c85071ff47768837130ef to your computer and use it in GitHub Desktop.
vec_drain.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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