-
Notifications
You must be signed in to change notification settings - Fork 32
/
struct-pattern-matching-with-methods.rs
61 lines (52 loc) · 1.38 KB
/
struct-pattern-matching-with-methods.rs
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
//@ edition:2021
//@check-pass
#![warn(unused)]
#![allow(dead_code)]
#![feature(rustc_attrs)]
#[derive(Debug, Clone, Copy)]
enum PointType {
TwoD { x: u32, y: u32 },
ThreeD{ x: u32, y: u32, z: u32 }
}
// Testing struct patterns
struct Points {
points: Vec<PointType>,
}
impl Points {
pub fn test1(&mut self) -> Vec<usize> {
(0..self.points.len())
.filter_map(|i| {
let idx = i as usize;
match self.test2(idx) {
PointType::TwoD { .. } => Some(i),
PointType::ThreeD { .. } => None,
}
})
.collect()
}
pub fn test2(&mut self, i: usize) -> PointType {
self.points[i]
}
}
fn main() {
let mut points = Points {
points: Vec::<PointType>::new()
};
points.points.push(PointType::ThreeD { x:0, y:0, z:0 });
points.points.push(PointType::TwoD{ x:0, y:0 });
points.points.push(PointType::ThreeD{ x:0, y:0, z:0 });
points.points.push(PointType::TwoD{ x:0, y:0 });
println!("{:?}", points.test1());
println!("{:?}", points.points);
}
// ferrocene-annotations: fls_7dbd5t2750ce
// Struct Patterns
//
// ferrocene-annotations: fls_asj8rgccvkoe
// Struct Pattern Matching
//
// ferrocene-annotations: fls_z7q8kbjwdc7g
// Method Call Expressions
//
// ferrocene-annotations: fls_wqazkzle0ix9
// Method Resolution