【问题标题】:Rust lifetime and calling member functionRust 生命周期和调用成员函数
【发布时间】:2014-02-09 04:56:52
【问题描述】:

我有一个关于 Rust 编程语言中变量生命周期的问题。

createTest 函数创建并返回右值引用。当它返回一个引用时,testValue 被销毁。但是 test.print() 不会导致崩溃。为什么?

(Test::print 函数是否被称为静态函数?)

代码

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");                                                         
}

结果

Dropped.
Print!
Test

【问题讨论】:

    标签: rust


    【解决方案1】:

    这对我来说似乎是一个错误。您的示例的简单扩展无法编译并带有绝对自然的错误消息:

    struct Test {
        x: int
    }
    
    impl Drop for Test {
      fn drop (&mut self) {
        println("Dropped.");
      }
    }
    
    impl Test {
      fn print(&self) { println!("Print: {}", self.x); }
    }
    
    fn createTest() -> &Test {
      let testValue = &Test{ x: 10 };
      testValue
    }
    
    fn main() {
      let test = createTest();
      test.print();
      println("Test");
    }
    

    错误信息:

    main.rs:16:19: 16:24 error: borrowed value does not live long enough
    main.rs:16   let testValue = &Test{ x: 10 };
                                 ^~~~~
    main.rs:15:26: 18:2 note: reference must be valid for the anonymous lifetime #1 defined on the block at 15:25...
    main.rs:15 fn createTest() -> &Test {
    main.rs:16   let testValue = &Test{ x: 10 };
    main.rs:17   testValue
    main.rs:18 }
    main.rs:15:26: 18:2 note: ...but borrowed value is only valid for the block at 15:25
    main.rs:15 fn createTest() -> &Test {
    main.rs:16   let testValue = &Test{ x: 10 };
    main.rs:17   testValue
    main.rs:18 }
    

    顺便说一句,当你想从函数返回某些东西时,你不必写return,除非它是提前返回(例如从循环内部)。只需在最后一条语句中省略分号即可。

    【讨论】:

      【解决方案2】:

      代码编译的原因是因为使用单元结构(即没有字段),基本上相当于:

       struct Test;
      
       static TestStatic: Test = Test;
      
       fn createTest() -> &Test {
            &TestStatic
       }
      

      所以&TestStatic 表达式的类型为&'static Test每个生命周期都是'static 的子生命周期,包括-> &Test 的隐式匿名生命周期,因此您可以在其位置返回'static

      此行为包含两个错误:


      也就是说,Drop 会这样运行是非常奇怪,所以感谢您提交11681

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-07-03
        • 1970-01-01
        • 2017-05-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多