summaryrefslogtreecommitdiff
path: root/patterns/src/main.rs
blob: 4ee101da4cf3ed858c8a74417055e99a2b5b1cca (plain) (blame)
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
fn main() {
//    let favorite_color: Option<&str> = None;
//    let is_tuesday = false;
//    let age: Result<u8, _> = "34".parse();
//
//    if let Some(color) = favorite_color {
//        dbg!(color);
//    }
//    else if is_tuesday {
//        dbg!("green");
//    }
//    else if let Ok(age) = age {
//        if age > 30 {
//            dbg!("purple");
//        }
//        else {
//            dbg!("orange");
//        }
//    }
//    else {
//        dbg!("blue");
//    }


//    let mut stack = Vec::new();
//
//    stack.push(1);
//    stack.push(2);
//    stack.push(3);
//
//    while let Some(top) = stack.pop() {
//        dbg!(top);
//    }


//    let v = vec!['a', 'b', 'c'];
//
//    for (_, value) in v.iter().enumerate() {
//        //println!("{} is at index {}", value, index);
//        dbg!(value);
//    }


//    let (..) = (1, 2, 3);


//    let point = (3, 5);
//    print_coords(&point);


//    let Some(x) = Some(5);
//    if let Some(x) = Some(5) {
//        dbg!(x);
//    }
//    if let x = 5 {
//        dbg!(x);
//    };


//    let x = 1;
//
//    match x {
//        1 => dbg!("one"),
//        2 => dbg!("two"),
//        3 => dbg!("three"),
//        _ => dbg!("else"),
//    };



//    let x = Some(5);
//    let y = 10;
//
//    match x {
//        Some(50) => println!("got 50"),
//        Some(y) => println!("matched, y == {:?}", y),
//        _ => println!("default: x == {:?}", x),
//    }
//
//    println!("end: x == {:?}, y == {:?}", x, y);


//    let x = 5;
//
//    match x {
//        1 ... 5 => println!("one through five"),
//        _ => println!("else"),
//    }


//    let x = 'w';
//
//    match x {
//        'a' ... 'j' => println!("early ascii char"),
//        'k' ... 'z' => println!("late ascii char"),
//        _ => println!("else"),
//    }



//    let p = Point { x: 0, y: 7 };
//    //let Point { x: a, y: b } = p;
//    let Point { x, y } = p;
//    assert_eq!(0, x);
//    assert_eq!(7, y);


//    let p = Point { x: 1, y: 0 };
//
//    match p {
//        Point { x, y: 0 } => dbg!("x-axis"),
//        Point { x: 0, y } => dbg!("y-axis"),
//        Point { x, y } => dbg!("neither"),
//    };


//    //let msg = Message::ChangeColor(0, 160, 255);
//    //let msg = Message::Write(String::from("hi there"));
//    //let msg = Message::Move {
//    //    x: 1,
//    //    y: 2,
//    //};
//    let msg = Message::ChangeColor(Color::Hsv(0, 160, 255));
//
//    match msg {
//        Message::Quit => {
//            println!("the quit variant has no data to destructure.")
//        },
//        Message::Move { x, y } => {
//            println!(
//                "Move in the direction {}, {}",
//                x,
//                y
//            );
//        }
//        Message::Write(text) => println!("text message: {}", text),
//        Message::ChangeColor(Color::Rgb(r, g, b)) => {
//            println!(
//                "Change the color to RGB {}{}{}", 
//                r,
//                g,
//                b
//            )
//        }
//        Message::ChangeColor(Color::Hsv(h, s, v)) => {
//            println!(
//                "Change the color to HSV {}{}{}",
//                h,
//                s,
//                v
//            )
//        }
//    }


//    let points = vec![
//        Point { x: 0, y: 0 },
//        Point { x: 1, y: 5 },
//        Point { x: 10, y: -3 },
//    ];
//
//    let sum_of_squares: i32 = points
//        .iter()
//        .map(|&Point { x, y } | x * x + y * y)
//        .sum();
//    dbg!(sum_of_squares);
//
//
//    let ((feet, inches), Point { x, y }) = ((3, 10), Point { x: 3, y: -10 });


//    foo(3, 4);


//    let mut setting_value = Some(5);
//    //let new_setting_value = Some(10);
//    let new_setting_value = None;
//
//    match (setting_value, new_setting_value) {
//        (Some(_), Some(_)) => {
//            println!("can't overwrite existing customized value.");
//        }
//        _ => {
//            setting_value = new_setting_value;
//        }
//    }
//
//    println!("setting is {:?}", setting_value);
//
//
//    let numbers = (2, 4, 8, 16, 32);
//
//    match numbers {
//        (first, _, third, _, fifth) => {
//            println!("numbers: {}, {}, {}", first, third, fifth)
//        },
//    }
//
//
//    let _x = 5;
//    let _y = 10;
//
//
//    let s = Some(String::from("hello"));
//
//    if let Some(_) = s {
//        println!("found a string");
//    }
//
//    println!("{:?}", s);


//    let origin = Point { x: 1, y: 2, z: 3 };
//
//    match origin {
//        Point {x, .. } => dbg!(x),
//    };
//
//    let numbers = (2, 4, 8, 16, 32);
//
//    match numbers {
//        (first, .., last) => {
//            dbg!(first);
//            dbg!(last);
//        },
//    }


//    let num = Some(4);
//
//    let test = match num {
//        Some(x) if x < 5 => println!("{}", x),
//        Some(x) => println!("{}", x),
//        None => (),
//    };


//    let x = Some(10);
//    let y = 10;
//
//    match x {
//        Some(50) => println!("got 50"),
//        Some(n) if n == y => println!("{:?}", n),
//        _ => println!("{:?}", x),
//    }
//
//    println!("{:?} {:?}", x, y);


//    let x = 4;
//    let y = false;
//
//    match x {
//        4 | 5 | 6 if y => println!("yes"),
//        _ => println!("no"),
//    }


    enum Message {
        Hello { id: i32 },
    }

    let msg = Message::Hello { id: 13 };

    match msg {
        Message::Hello { id: id_variable @ 3...7 } => {
            println!("{}", id_variable)
        },
        Message::Hello { id: 10...12 } => {
            println!("another range")
        },
        Message::Hello { id } => {
            println!("else")
        },
    }


    println!("Done.");
}


//struct Point {
//    x: i32,
//    y: i32,
//    z: i32,
//}


//fn foo(_: i32, y: i32) {
//    println!("this code only uses y: {}", y);
//}


//fn print_coords(&(x, y): &(i32, i32)) {
//    println!("{}, {}", x, y);
//}


//struct Point {
//    x: i32,
//    y: i32,
//}


//enum Message {
//    Quit,
//    Move { x: i32, y: i32 },
//    Write(String),
//    ChangeColor(Color),
//}
//
//
//enum Color {
//    Rgb(i32, i32, i32),
//    Hsv(i32, i32, i32)
//}