8 let b = Some(|| println!("{}", a));
9 // When we capture by reference we can use any of the
10 // captures as the discriminant since they're all
12 assert_eq!(mem::size_of_val(&b), mem::size_of::<usize>());
15 let a = Box::new(12i32);
16 let b = Some(move || println!("{}", a));
17 // We captured `a` by value and since it's a `Box` we can use it
18 // as the discriminant.
19 assert_eq!(mem::size_of_val(&b), mem::size_of::<Box<i32>>());
21 // By Value Capture - Transitive case
22 let a = "Hello".to_string(); // String -> Vec -> Unique -> NonZero
23 let b = Some(move || println!("{}", a));
24 // We captured `a` by value and since down the chain it contains
25 // a `NonZero` field, we can use it as the discriminant.
26 assert_eq!(mem::size_of_val(&b), mem::size_of::<String>());
28 // By Value - No Optimization
30 let b = Some(move || println!("{}", a));
31 // We captured `a` by value but we can't use it as the discriminant
32 // thus we end up with an extra field for the discriminant
33 assert_eq!(mem::size_of_val(&b), mem::size_of::<(i32, i32)>());