-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlifetime.rs
52 lines (43 loc) · 1.12 KB
/
lifetime.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
use std::collections::HashMap;
struct Person<'a> {
name: &'a str,
location: &'a str,
}
// struct Person<'a> {
// name: String,
// location: &'a String,
// }
fn main() {
let airport_codes = HashMap::from([
("WAW", "Warsaw"),
("WMI", "Modlin"),
("PMI", "Palma de Mallorca"),
]);
let tomek = Person {
name: "Tomek",
location: "WMI",
};
fn get_airport_name<'a>(code: &'a str, airport_codes: &'a HashMap<&str, &str>) -> &'a str {
airport_codes
.get(code)
.expect("No airport found for that code.")
}
println!(
"Welcome, {} from {}!",
tomek.name,
get_airport_name(&tomek.location, &airport_codes)
);
}
// fn main() {
// let london = String::from("London");
// let me = Person {
// name: String::from("Sid"),
// location: &london,
// };
// let jon = Person {
// name: String::from("Jon"),
// location: &london,
// };
// println!("Hello, {} from {}!", me.name, me.location);
// println!("Hello, {} from {}!", jon.name, jon.location);
// }