1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
/// # 零大小类型: /// /// Base usage: /// /// ``` /// fn zero_size(){ /// enum Void {} /// struct Foo; /// struct Baz { /// foo: Foo, /// qux: (), /// baz: [u8; 0], /// } /// assert_eq!(std::mem::size_of::<()>(), 0); /// assert_eq!(std::mem::size_of::<Foo>(), 0); /// assert_eq!(std::mem::size_of::<Baz>(), 0); /// assert_eq!(std::mem::size_of::<Void>(), 0); /// assert_eq!(std::mem::size_of::<[(); 10]>(), 0); /// } /// zero_size(); /// ``` pub fn zero_size(){ use std::mem::size_of; enum Void {} struct Foo; struct Baz { foo: Foo, qux: (), baz: [u8; 0], } assert_eq!(size_of::<()>(), 0); assert_eq!(size_of::<Foo>(), 0); assert_eq!(size_of::<Baz>(), 0); assert_eq!(size_of::<Void>(), 0); assert_eq!(size_of::<[(); 10]>(), 0); } /// # 零大小类型:应用 /// /// Base usage: 利用单元类型查看值的类型 /// /// ``` /// let v: () = vec![(); 10]; // Error: expected (), found struct `std::vec::Vec` /// ``` /// /// Base usage: 零大小循环 /// /// ``` /// fn zero_size_loop(){ /// let v: Vec<()> = vec![(); 10]; /// for i in v { /// println!("{:?}", i); /// } /// } /// zero_size_loop(); /// ``` pub fn zero_size_loop(){ let v: Vec<()> = vec![(); 10]; for i in v { println!("{:?}", i); } }