我有一个关于Rust编程语言中的可变寿命的问题。锈的生命期和调用成员函数
createTest
函数创建并返回r值引用。当它返回一个引用时,testValue
被销毁。但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