File tree 1 file changed +23
-2
lines changed
1 file changed +23
-2
lines changed Original file line number Diff line number Diff line change @@ -134,8 +134,29 @@ x: &'a i32,
134
134
# }
135
135
```
136
136
137
- uses it. So why do we need a lifetime here? We need to ensure that any
138
- reference to the contained ` i32 ` does not outlive the containing ` Foo ` .
137
+ uses it. So why do we need a lifetime here? We need to ensure that any reference
138
+ to a ` Foo ` cannot outlive the reference to an ` i32 ` it contains.
139
+
140
+ If you have multiple references, you can use the same lifetime multiple times:
141
+
142
+ ``` rust
143
+ fn x_or_y <'a >(x : & 'a str , y : & 'a str ) -> & 'a str {
144
+ # x
145
+ # }
146
+ ```
147
+
148
+ This says that ` x ` and ` y ` both are alive for the same scope, and that the
149
+ return value is also alive for that scope. If you wanted ` x ` and ` y ` to have
150
+ different lifetimes, you can use multiple lifetime parameters:
151
+
152
+ ``` rust
153
+ fn x_or_y <'a , 'b >(x : & 'a str , y : & 'b str ) -> & 'a str {
154
+ # x
155
+ # }
156
+ ```
157
+
158
+ In this example, ` x ` and ` y ` have different valid scopes, but the return value
159
+ has the same lifetime as ` x ` .
139
160
140
161
## Thinking in scopes
141
162
You can’t perform that action at this time.
0 commit comments