//! Comprehensive Vector (Vec<T>) method examples
pub fn demonstrate_vector_methods() {
println!("\n=== VECTOR CREATION ===");
// 1. Different ways to create vectors
let vec1 = vec![1, 2, 3]; // Macro creation
let mut vec2 = Vec::new(); // Empty vector
vec2.push(4);
vec2.push(5);
let vec3 = Vec::with_capacity(10); // Pre-allocated
println!("vec1: {:?}", vec1);
println!("vec2: {:?}", vec2);
println!("vec3 capacity: {}", vec3.capacity());
println!("\n=== ELEMENT ACCESS ===");
// 2. Accessing elements
println!("First element: {:?}", vec1.first());
println!("Last element: {:?}", vec1.last());
println!("Index 1: {:?}", vec1.get(1));
println!("Direct index: {}", vec1[0]);
println!("\n=== MODIFICATION ===");
// 3. Adding elements
let mut vec = vec![1, 2];
vec.push(3); // Add to end
vec.insert(1, 4); // Insert at index
println!("After modifications: {:?}", vec);
// 4. Removing elements
vec.pop(); // Remove from end
vec.remove(1); // Remove at index
println!("After removals: {:?}", vec);
println!("\n=== ITERATION ===");
// 5. Iteration methods
println!("Standard iteration:");
for v in &vec {
print!("{} ", v);
}
println!("\n\nEnumerated iteration:");
for (i, v) in vec.iter().enumerate() {
println!("Index {}: {}", i, v);
}
println!("\n=== SEARCHING ===");
// 6. Searching methods
println!("Contains 2? {}", vec.contains(&2));
println!("Position of 1: {:?}", vec.iter().position(|&x| x == 1));
println!("Find even: {:?}", vec.iter().find(|&&x| x % 2 == 0));
println!("\n=== TRANSFORMATION ===");
// 7. Transformation methods
let squared: Vec<_> = vec.iter().map(|x| x * x).collect();
println!("Squared: {:?}", squared);
let filtered: Vec<_> = vec.iter().filter(|&x| x > &1).copied().collect();
println!("Filtered: {:?}", filtered);
println!("\n=== SORTING ===");
// 8. Sorting methods
let mut to_sort = vec![3, 1, 4, 2];
to_sort.sort();
println!("Sorted: {:?}", to_sort);
to_sort.sort_by(|a, b| b.cmp(a));
println!("Reverse sorted: {:?}", to_sort);
println!("\n=== MEMORY MANAGEMENT ===");
// 9. Capacity methods
println!("Length: {}", vec.len());
println!("Capacity: {}", vec.capacity());
vec.reserve(10);
println!("After reserve(10): {}", vec.capacity());
vec.shrink_to_fit();
println!("After shrink_to_fit: {}", vec.capacity());
println!("\n=== ADVANCED OPERATIONS ===");
// 10. Advanced operations
let mut vec_a = vec![1, 2, 3];
let mut vec_b = vec![4, 5];
vec_a.append(&mut vec_b);
println!("After append: {:?}", vec_a);
println!("vec_b is now: {:?}", vec_b);
let drained: Vec<_> = vec_a.drain(1..3).collect();
println!("After drain: {:?}", vec_a);
println!("Drained items: {:?}", drained);
vec_a.retain(|&x| x > 1);
println!("After retain: {:?}", vec_a);
}
// Remember to add to src/menu_items/mod.rs:
// pub mod vector_methods;