- 
                Notifications
    
You must be signed in to change notification settings  - Fork 13.9k
 
Labels
A-destructorsArea: Destructors (`Drop`, …)Area: Destructors (`Drop`, …)A-lifetimesArea: Lifetimes / regionsArea: Lifetimes / regionsE-needs-testCall for participation: An issue has been fixed and does not reproduce, but no test has been added.Call for participation: An issue has been fixed and does not reproduce, but no test has been added.
Milestone
Description
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
Labels
A-destructorsArea: Destructors (`Drop`, …)Area: Destructors (`Drop`, …)A-lifetimesArea: Lifetimes / regionsArea: Lifetimes / regionsE-needs-testCall for participation: An issue has been fixed and does not reproduce, but no test has been added.Call for participation: An issue has been fixed and does not reproduce, but no test has been added.