1
0
mirror of https://github.com/google/comprehensive-rust.git synced 2025-04-01 17:34:25 +02:00

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.
This commit is contained in:
Nicole L 2024-05-23 16:27:10 -07:00 committed by GitHub
parent 5ce6a9bd72
commit 6115a12554
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -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;
}
```