blob: d95b6844d755df769eeaadc1d7abbc5f6ae13c7a [file] [log] [blame]
Matthias Andreas Benkard87dfeb72019-02-28 06:21:29 +01001#![allow(dead_code)]
2
3struct Person {
4 age: i32,
5 name: String,
6}
7
8impl ToString for Person {
9 fn to_string(&self) -> String {
10 format!("{} ({})", self.name, self.age)
11 }
12}
13
14fn greet_person_1(p: Person) {
15 println!("Hello {} ({})!", p.name, p.age);
16}
17
18fn greet_person_2(p: &Person) {
19 println!("Hello {} ({})!", p.name, p.age);
20}
21
22fn birthday(p: &mut Person) {
23 p.age += 1;
24}
25
26fn 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}