Skip to content

Empty struct lifetime bug #11681

@angdev

Description

@angdev

I asked stackoverflow about lifetime of variable. Original link is this : http://stackoverflow.com/questions/21226942/rust-lifetime-and-calling-member-function

** Question from link **

I have a question about lifetime of varable in Rust programming language.

createTest function creates and returns r-value reference. and when it returns a reference, testValue is destroyed. But test.print() doesn't lead to crash. Why?

(Is Test::print function called as static function?)

Code

struct Test;                                                         
impl Drop for Test {                                                       
  fn drop (&mut self) {                                                    
    println("Dropped.");                                                   
  }                                                                        
}                                                                          

impl Test {                                                                
  fn print(&self) { println!("Print!"); }                                  
}                                                                          

fn createTest() -> &Test {                                                 
  let testValue = &Test;                                                   
  return testValue;                                                        
}                                                                          

fn main() {                                                                
  let test = createTest();                                             
  test.print();                                                            
  println("Test");                                                         
}

Result

Dropped.
Print!
Test

It happened with empty struct only. Is it a really bug?

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-destructorsArea: Destructors (`Drop`, …)A-lifetimesArea: Lifetimes / regionsE-needs-testCall for participation: An issue has been fixed and does not reproduce, but no test has been added.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions