【发布时间】: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