-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlifetimes.rs
More file actions
62 lines (56 loc) · 1.5 KB
/
lifetimes.rs
File metadata and controls
62 lines (56 loc) · 1.5 KB
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
fn main() {
does_not_compile();
compiles();
test_compare_nums();
}
fn test_compare_nums() {
let result: &i32;
let first = 13;
{
let second = -12;
result = compare_two_nums(&first, &second);
println!("The larger num is : {result}");
}
// println!("The larger num in the outer scope is : {result}");
}
fn does_not_compile() {
let result: &str;
let first = "Short string slice".to_string();
{
let second = "Really looonnngggg string slice....".to_string();
result = compare_strings(&first, &second);
println!("The result is {result}");
}
// println!("The result in the outer scope is {result}"); // DOES NOT COMPILE IF UNCOMMENTED.
}
fn compiles() {
let result: &str;
let first = "Short string slice";
{
let second = "Really looonnngggg string slice....";
result = compare_string_slices(first, second);
println!("The result is {result}");
}
println!("The result in the outer scope is {result}");
}
fn compare_string_slices<'a>(first: &'a str, second: &'a str) -> &'a str {
if first.len() > second.len() {
return first;
} else {
return second;
}
}
fn compare_strings<'a>(first: &'a String, second: &'a String) -> &'a str {
if first.len() > second.len() {
return first;
} else {
return second;
}
}
fn compare_two_nums<'a>(x: &'a i32, y: &'a i32) -> &'a i32 {
if x > y {
return x;
} else {
return y;
}
}