fn foo1(v1: &[i32]) -> &[i32] {
let v = &[1, 2, 3];
v
}
fn foo2(v1: &[i32]) -> &[i32] {
let v = [1, 2, 3];
&v
}
该代码将导致以下错误消息:
error[E0515]: cannot return reference to local variable `v`
--> src/lib.rs:8:5
|
8 | &v
| ^^ returns a reference to data owned by the current function
error: aborting due to previous error
在我看来,这两个函数是相同的,但是它们的行为并不相同。为什么?