How does Rust treat structures as function parameters and return values?

I have some experience working in C, but I'm new to Rust. What happens under the hood when I pass the structure to the function and return the structure from the function? This does not seem to “copy” the structure, but if it is not copied, then where is the structure created? Is it a stack of external function?

struct Point {
    x: i32,
    y: i32,
}

// I know it 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); 
}
+4
source share
2 answers

How long has the C programmer been playing with Rust lately too, I understand where you come from. It’s important for me to understand that in Rust, the value of vs refers to ownership, and the compiler can set up calling conventions to optimize the semantics of movement.

, , , . , C ABI , , .

, , , . , C .

, ABI/ . , , , .

+8

, .

, , , ( Copy). , .

, , , . , - memcpy, . , - , .

0

Source: https://habr.com/ru/post/1626140/


All Articles