fn main() { let v1 = vec![1, 2, 3]; let v1_iter = v1.iter(); for val in v1_iter { println!("Got: {}", val); } } #[cfg(test)] mod tests { use std::mem::size_of_val; #[test] fn iterator_demonstration() { let v1 = vec![1, 2, 3]; let mut v1_iter = v1.iter(); assert_eq!(v1_iter.next(), Some(&1)); assert_eq!(v1_iter.next(), Some(&1)); assert_eq!(v1_iter.next(), Some(&1)); assert_eq!(v1_iter.next(), None); } #[test] fn iterator_consumption() { let v1 = vec![1, 2, 3]; let v1_iter = v1.iter(); let s: i32 = v1_iter.sum(); println!("{s}"); assert_eq!(s, 6) } #[test] fn iterator_another_way_get() { let mut v1: Vec = vec![1, 2, 3]; let v1_iter = v1.iter_mut(); let res: Vec<&mut u32> = v1_iter .map(|x| { *x = *x * 3; x }) .collect(); println!("res: {:?}, sizeof: {}, addr : {:x}", res, size_of_val(&res), res.as_ptr() as usize); println!("v1: {:?}, sizeof: {}, addr: {:x}", v1, size_of_val(&v1), v1.as_ptr() as usize); assert!(false); } }