summaryrefslogtreecommitdiff
path: root/advanced/adv-traits/src/main.rs
blob: 659883c492c5b5733ab5a3458f732f1aac3fc4e9 (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
//fn main() {
//    println!("Hello, world!");
//}
//
//pub trait Iterator {
//    type Item;
//
//    fn next(&mut self) -> Option<Self::Item>;
//}
//
//
//#[derive(Debug, PartialEq)]
//struct Point {
//    x: i32,
//    y: i32,
//}
//
//impl Add for Point {
//    type Output = Point;
//
//    fn add(self, other: Point) -> Point {
//        Point {
//            x: self.x + other.x,
//            y: self.y + other.y,
//        }
//    }
//}

//fn main() {
//    assert_eq!(Point { x: 1, y: 0 } + Point { x: 2, y: 3 },
//               Point { x: 3, y: 3 });
//    println!("Done.");
//}

//trait Add<RHS=Self> {
//    type Output;
//
//    fn add(self, rhs: RHS) -> Self::Output;
//}

//use std::ops::Add;
//
//struct Millimeters(u32);
//struct Meters(u32);
//
//impl Add<Meters> for Millimeters {
//    type Output = Millimeters;
//
//    fn add(self, other: Meters) -> Millimeters {
//        Millimeters(self.0 + (other.0 * 1000))
//    }
//}


//trait Pilot {
//    fn fly(&self);
//}
//
//trait Wizard {
//    fn fly(&self);
//}
//
//struct Human;
//
//impl Pilot for Human {
//    fn fly(&self) {
//        println!("Because I was inverted.");
//    }
//}
//
//impl Wizard for Human {
//    fn fly(&self) {
//        println!("Fly, you fools.");
//    }
//}
//
//impl Human {
//    fn fly(&self) {
//        println!("*feels real loose like a long-neck goose*");
//    }
//}
//
//
//trait Animal {
//    fn baby_name() -> String;
//}
//
//struct Dog;
//
//impl Dog {
//    fn baby_name() -> String {
//        String::from("Spot")
//    }
//}
//
//impl Animal for Dog {
//    fn baby_name() -> String {
//        String::from("puppy")
//    }
//}


//fn main() {
//    let person = Human;
//    person.fly();
//    Pilot::fly(&person);
//    Wizard::fly(&person);
//
//
//    println!("A baby dog is called a {}", <Dog as Animal>::baby_name());
//
//
//    println!("Done.");
//}
//


//use std::fmt;
//
//trait OutlinePrint: fmt::Display {
//    fn outline_print(&self) {
//        let output = self.to_string();
//        let len = output.len();
//        println!("{}", "*".repeat(len + 4));
//        println!("*{}*", " ".repeat(len + 2));
//        println!("* {} *", output);
//        println!("*{}*", " ".repeat(len + 2));
//        println!("{}", "*".repeat(len + 4));
//    }
//}
//
//struct Point { 
//    x: i32,
//    y: i32,
//}
//
//impl OutlinePrint for Point {}
//
//impl fmt::Display for Point {
//    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
//        write!(f, "({}, {})", self.x, self.y)
//    }
//}
//
//fn main() {
//    let point = Point { x: 5, y: 6 };
//    point.outline_print();
//}

use std::fmt;

struct Wrapper(Vec<String>);

impl fmt::Display for Wrapper {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "[{}]", self.0.join(", "))
    }
}

fn main() {
    let w = Wrapper(vec![String::from("hello"), String::from("world")]);
    println!("w = {}", w);
}