2

I return a reference of a temporary value from some function. I'm not expecting this to compile successfully, however, I got no warning or error and the program works well.

struct A {
    pub a: i32,
}

fn main() {
    let b = A { a: 5 };
    let a = some(&b);
    println!("{}", a.a);
}

fn some(_: &A) -> &A {
    &A { a: 14 }
}

impl Drop for A or return &mut A will cause this program cannot compile. I am using rustc 1.35.0-nightly.

Is this problem caused by some optimization? Why will optimization lead to this "grammar feature"?

YangKeao
  • 184
  • 6
  • I hope the duplicate resolves your question. If not, feel free to edit your question and ping me. – hellow Apr 15 '19 at 06:32

0 Answers0