Skip to content

Commit

Permalink
Fix missing lifetime error in dangling reference example (#2093)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
randomPoison committed May 23, 2024
1 parent 5ce6a9b commit 6115a12
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/references/shared.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ Rust will statically forbid dangling references:
<!-- mdbook-xgettext: skip -->

```rust,editable,compile_fail
fn x_axis(x: i32) -> &(i32, i32) {
let point = (x, 0);
fn x_axis(x: &i32) -> &(i32, i32) {
let point = (*x, 0);
return &point;
}
```
Expand Down

0 comments on commit 6115a12

Please sign in to comment.