1 | use std::mem; |
---|
2 | |
---|
3 | enum WeekO { Mon, Tue, Wed, Thu, Fri, Sat, Sun } |
---|
4 | #[derive(Debug, Clone, Copy)] |
---|
5 | enum Week { Mon, Tue, Wed = 2, Thu = 10, Fri, Sat, Sun } |
---|
6 | |
---|
7 | #[derive(Debug)] |
---|
8 | struct S { |
---|
9 | i : isize, j : isize, |
---|
10 | } |
---|
11 | #[repr(u8)] |
---|
12 | #[derive(Debug)] |
---|
13 | enum ADT { |
---|
14 | I(isize) = 5, |
---|
15 | F(f64) = 10, |
---|
16 | S(S) = 0, |
---|
17 | } |
---|
18 | |
---|
19 | #[derive(Debug)] |
---|
20 | enum Animal { |
---|
21 | Dog( String, f64 ), |
---|
22 | Cat{ name: String, weight: f64 }, |
---|
23 | } |
---|
24 | |
---|
25 | #[derive(Debug)] |
---|
26 | enum Direction { North, South, East, West } |
---|
27 | |
---|
28 | impl Direction { |
---|
29 | const VALUES: [Self; 4] = [Self::North, Self::South, Self::East, Self::West]; |
---|
30 | } |
---|
31 | #[repr(u8)] |
---|
32 | enum Fieldless { |
---|
33 | Tuple() = 5, |
---|
34 | Struct{} = 10, |
---|
35 | Unit = 12, |
---|
36 | } |
---|
37 | impl Fieldless { |
---|
38 | fn discriminant(&self) -> u8 { |
---|
39 | // SAFETY: Because `Self` is marked `repr(u8)`, its layout is a `repr(C)` `union` |
---|
40 | // between `repr(C)` structs, each of which has the `u8` discriminant as its first |
---|
41 | // field, so we can read the discriminant without offsetting the pointer. |
---|
42 | unsafe { *<*const _>::from(self).cast::<u8>() } |
---|
43 | } |
---|
44 | } |
---|
45 | fn main() { |
---|
46 | let arr : [i32; 7]; |
---|
47 | |
---|
48 | let mut s = S{ i : 3, j : 4 }; |
---|
49 | let mut adt : ADT; |
---|
50 | adt = ADT::I(3); println!( "{:?}", adt ); |
---|
51 | adt = ADT::F(3.5); println!( "{:?}", adt ); |
---|
52 | adt = ADT::S(s); println!( "{:?}", adt ); |
---|
53 | match adt { |
---|
54 | ADT::I(i) => println!( "{:}", i ), |
---|
55 | ADT::F(f) => println!( "{:}", f ), |
---|
56 | ADT::S(s) => println!( "{:} {:}", s.i, s.j ), |
---|
57 | } |
---|
58 | |
---|
59 | let mut wo : WeekO = WeekO::Mon; |
---|
60 | match wo { |
---|
61 | WeekO::Mon => println!( "Mon" ), |
---|
62 | _ => (), |
---|
63 | } |
---|
64 | |
---|
65 | let mut fl : Fieldless; |
---|
66 | fl = Fieldless::Struct{}; |
---|
67 | match fl { |
---|
68 | Fieldless::Struct{} => println!( "Struct" ), |
---|
69 | _ => (), |
---|
70 | } |
---|
71 | if fl.discriminant() == 10 { |
---|
72 | println!( "Struct" ); |
---|
73 | } |
---|
74 | let mut week: Week = Week::Sat; |
---|
75 | match week { |
---|
76 | Week::Mon => println!( "X {:?}", Week::Mon as isize ), |
---|
77 | Week::Tue => println!( "X {:?}", Week::Tue as isize ), |
---|
78 | Week::Wed => println!( "X {:?}", Week::Wed as isize ), |
---|
79 | Week::Thu => println!( "X {:?}", Week::Thu as isize ), |
---|
80 | Week::Fri => println!( "X {:?}", Week::Fri as isize ), |
---|
81 | Week::Sat => println!( "X {:?}", Week::Sat as isize ), |
---|
82 | Week::Sun => println!( "X {:?}", Week::Sun as isize ), |
---|
83 | } |
---|
84 | match week { |
---|
85 | Week::Mon | Week:: Tue | Week::Wed | Week::Thu | Week::Fri => println!( "weekday" ), |
---|
86 | Week::Sat | Week:: Sun => println!( "weekend" ), |
---|
87 | } |
---|
88 | println!( "{:?} {:?}", week as isize, Week::Fri as isize ); |
---|
89 | if mem::discriminant(&week) == mem::discriminant(&Week::Mon) { |
---|
90 | println!( "{:?}", week ); |
---|
91 | } |
---|
92 | if week as isize == Week::Mon as isize { |
---|
93 | println!( "{:?}", week ); |
---|
94 | } |
---|
95 | for d in Week::Mon as isize ..= Week::Sun as isize { |
---|
96 | print!( "{:?} ", d ); |
---|
97 | } |
---|
98 | println!( "" ); |
---|
99 | let mut mon : isize = Week::Mon as isize; |
---|
100 | |
---|
101 | week = Week::Fri; |
---|
102 | println!( "{:?}", week ); |
---|
103 | |
---|
104 | let mut a: Animal = Animal::Dog("Cocoa".to_string(), 37.2); |
---|
105 | println!( "{:?}", a ); |
---|
106 | a = Animal::Cat{ name: "Spotty".to_string(), weight: 2.7 }; |
---|
107 | // println!( "{:?} {:?}", a, a.name ); |
---|
108 | |
---|
109 | for direction in Direction::VALUES { |
---|
110 | println!("{direction:?}"); |
---|
111 | } |
---|
112 | } |
---|
113 | |
---|
114 | // Local Variables: // |
---|
115 | // tab-width: 4 // |
---|
116 | // End: // |
---|