Skip to content

Commit 5e94187

Browse files
committed
Add syntax for multiple lifetimes
Fixes #25417
1 parent 2d447e4 commit 5e94187

File tree

1 file changed

+23
-2
lines changed

1 file changed

+23
-2
lines changed

src/doc/trpl/lifetimes.md

+23-2
Original file line numberDiff line numberDiff line change
@@ -134,8 +134,29 @@ x: &'a i32,
134134
# }
135135
```
136136

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`.
139160

140161
## Thinking in scopes
141162

0 commit comments

Comments
 (0)