Matthias Andreas Benkard | 87dfeb7 | 2019-02-28 06:21:29 +0100 | [diff] [blame^] | 1 | #![allow(dead_code)] |
| 2 | |
| 3 | struct Person { |
| 4 | age: i32, |
| 5 | name: String, |
| 6 | } |
| 7 | |
| 8 | impl ToString for Person { |
| 9 | fn to_string(&self) -> String { |
| 10 | format!("{} ({})", self.name, self.age) |
| 11 | } |
| 12 | } |
| 13 | |
| 14 | fn greet_person_1(p: Person) { |
| 15 | println!("Hello {} ({})!", p.name, p.age); |
| 16 | } |
| 17 | |
| 18 | fn greet_person_2(p: &Person) { |
| 19 | println!("Hello {} ({})!", p.name, p.age); |
| 20 | } |
| 21 | |
| 22 | fn birthday(p: &mut Person) { |
| 23 | p.age += 1; |
| 24 | } |
| 25 | |
| 26 | fn main() { |
| 27 | // -- Struct instantiation -- |
| 28 | let mut p = Person { age: 30, name: "Mary".to_string() }; |
| 29 | |
| 30 | // -- Borrowing -- |
| 31 | //greet_person_1(p); |
| 32 | //greet_person_1(p); |
| 33 | |
| 34 | greet_person_2(&p); |
| 35 | greet_person_2(&p); |
| 36 | |
| 37 | birthday(&mut p); |
| 38 | |
| 39 | greet_person_2(&p); |
| 40 | |
| 41 | // -- Traits -- |
| 42 | println!("p = {}", p.to_string()); |
| 43 | |
| 44 | // -- Debugging -- |
| 45 | let x = dbg!(100 + 2) + 3; |
| 46 | println!("Hello, world! 100 + 2 + 3 = {}", x); |
| 47 | } |