我有一个关于Rust编程语言中varable的生存期的问题。
createTest
函数创建并返回r值参考。当它返回引用时,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
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句