我有一些C语言的经验,但是对于Rust来说还很新。当我将一个结构体传递到一个函数中并从函数中返回一个结构体时,在底层会发生什么?它似乎并没有“复制”结构体,但如果没有复制,那么结构体在哪里创建?它是在外部函数的堆栈中吗?
struct Point {
x: i32,
y: i32,
}
// I know it's better to pass in a reference here,
// but I just want to clarify the point.
fn copy_struct(p: Point) {
// Is this return value created in the outer stack
// so it won't be cleaned up while exiting this function?
Point {.. p}
}
fn test() {
let p1 = Point { x: 1, y: 2 };
// Will p1 be copied or does copy_struct
// just use a reference of the one created on the outer stack?
let p2 = copy_struct(p1);
}