Skip to content

Commit d71581e

Browse files
author
Colin MacRae
committed
completed lecture 31 - dynamic dispatch
1 parent ec47b4f commit d71581e

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

src/main.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
use std::mem;
2+
3+
trait Printable {
4+
fn format(&self) -> String;
5+
}
6+
7+
impl Printable for i32 {
8+
fn format(&self) -> String {
9+
format!("i32: {}", *self)
10+
}
11+
}
12+
13+
impl Printable for String {
14+
fn format(&self) -> String {
15+
format!("string: {}", *self)
16+
}
17+
}
18+
19+
fn print_it(z: &Printable) {
20+
println!("{}", z.format());
21+
}
22+
23+
fn main() {
24+
let a = 123;
25+
let b = "hello".to_string();
26+
27+
// println!("{}", a.format());
28+
// println!("{}", b.format());
29+
print_it(a);
30+
print_it(b);
31+
}

0 commit comments

Comments
 (0)