-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexplicit_lifetime.rs
More file actions
74 lines (50 loc) · 1.9 KB
/
explicit_lifetime.rs
File metadata and controls
74 lines (50 loc) · 1.9 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
63
64
65
66
67
68
69
70
71
72
73
#![allow(dead_code)]
/* Rust's borrow checker uses these annotations to ensure that a program does not have any invalid memory references, which can cause a range of issues from null pointer dereferences to data races.
By specifying the lifetimes of references, Rust can guarantee that a compiled program will be safe and free of these common issues.
Explicit lifetime annotations are especially important when working with structs that contain references, as their lifetimes must be carefully managed to prevent dangling pointers and other unsafe conditions. */
struct Foo {
x: i32,
}
//life time of the datatypes
//defining a struct called LifeTime
struct LifeTime<'a>{
x:&'a i32
}
//single life time example
fn do_something_single<'a>(foo: &'a Foo) -> &'a i32 {
return &foo.x;
}
//multiple life time example
// foo_b and the return value share the same lifetime
// foo_a has an unrelated lifetime
fn do_something_multiple<'a,'b>(foo_a: &'a Foo, foo_b: &'b Foo)-> &'b i32 {
println!("{}",foo_a.x);
println!("{}",foo_b.x);
return &foo_b.x;
//lifetime 'b bounds both do_something_multiple and foo_b which means foo_b exits until do_something_multiple exits
}
fn main()
{
println!("---single lifetime---");
let mut foo = Foo { x: 42 };
let x = &mut foo.x;
*x = 13;
let y= do_something_single(&foo);
println!("{}",y);
//multiple lifetime
let foo_a = Foo {x:42};
let foo_b = Foo {x:43};
println!("---multiple lifetime---");
let x =do_something_multiple(&foo_a, &foo_b);
// foo_a is dropped here because only foo_b's lifetime exist beyond here
println!("{}",x);
// x is dropped here
// foo_b is dropped here
//life time of a datatype
println!("---lifetime of a datatype---");
let x = 12;
let datatype_lifetime_a = LifeTime{
x :&x
};
println!("{}",datatype_lifetime_a.x);
}