1

I read the explanation of E0716, but still can't thoroughly understand the following code. Why it can be compiled?

#[derive(Debug)]
struct X {
    a: i32,
    b: i32,
}
let y;
y = &X { a: 1, b: 2 };    // Why didn't it break E0716?
println!("{:?}", y);

or

#[derive(Debug)]
struct X {
    a: i32,
    b: i32,
}
let y;
{
    y = &X { a: 1, b: 2 };
}
println!("{:?}", y);
Clark Guan
  • 11
  • 1
  • Welcome to Stack Overflow! This is a fairly common thing to wonder about; in fact, it's been asked before a couple times, so we already have a ready answer for it. I'm going to mark your question as a duplicate of that one so future askers with the same question will find it too. – trent Nov 06 '20 at 03:09
  • Thanks! Is there a detailed explanation about ''? – Clark Guan Nov 06 '20 at 03:44
  • `constexpr` is a C++ keyword for "constant expression" -- i.e. an expression that can be evaluated by the compiler at compile time (because it does not rely on any runtime information). [In Rust we use the `const` keyword for those things](https://stackoverflow.com/q/41346532/3650362). I'm not sure why the RFC uses "constexpr" instead. – trent Nov 06 '20 at 11:09

0 Answers0