What is the difference makes the behavior different when using temporary values?

Consider such two use

struct Test{
impl Test{
  fn get_i(&self)->&i32{
fn get_test()->Test{
fn main(){
   let i = &get_test().i; // Ok
   let i2 = get_test().get_i(); //error

The second use can cause the error that:

temporary value dropped while borrowed

I feel the first use is a bit like the concept in C++ that the reference bound can extend the lifetime of the temporary object in some scenes. I'm not sure whether Rust has a similar behavior (might it be). If it has, what is the context in which the temporary lifetime will be extended?

Temporary lifetime extension is documented here:

The rules in that link are a bit hard to comprehend. Is there any simple way to know whether the lifetime of a temporary value is extended?

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.