Skip to content

Commit 6115a12

Browse files
authored
Fix missing lifetime error in dangling reference example (#2093)
The example of returning a reference to a local variable doesn't compile due to a missing lifetime specifier, which isn't what we're trying to demonstrate with that example. I usually add the lifetime in manually in order to demonstrate the compiler error, but it occurs to me that if we make the argument a reference we can sneakily get the correct compiler error without having to introduce the lifetime syntax.
1 parent 5ce6a9b commit 6115a12

File tree

1 file changed

+2
-2
lines changed

1 file changed

+2
-2
lines changed

src/references/shared.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ Rust will statically forbid dangling references:
3030
<!-- mdbook-xgettext: skip -->
3131

3232
```rust,editable,compile_fail
33-
fn x_axis(x: i32) -> &(i32, i32) {
34-
let point = (x, 0);
33+
fn x_axis(x: &i32) -> &(i32, i32) {
34+
let point = (*x, 0);
3535
return &point;
3636
}
3737
```

0 commit comments

Comments
 (0)