Why temporary behave differently?

this code compile

fn main() {
  struct U(u8);
  fn test(x:&U)->&U {
    x
  }
  let x = test(&U(0));
  println!("{}",x.0);
}

but this code does not compile when implemented Drop

fn main() {
  struct U(u8);
  fn test(x:&U)->&U {
    x
  }
  let x = test(&U(0));
  println!("{}",x.0);
  
  impl Drop for U {
    fn drop(&mut self) {
      println!("drop U");
    }
  }
}

This is due to static promotion where references to compile-time constants are turned into references to immutable globals. Your first example compiles to this:

fn main() {
    struct U(u8);
    fn test(x: &U) -> &U {
        x
    }
    
    static U_ZERO: U = U(0);
    
    let x = test(&U_ZERO);
    println!("{}", x.0);
}

However when the type has a destructor this does not happen.

thank you. I'll check it.