2020-09-25: Point in rust is a structure type, [let p1=Point{x:25,y:25}; let p2=p1;] Are p1 and p2 the same object?

Fu Ge Answer 2020-09-25: #福大Architects Daily One Question#

Not the same object. p1 and p2 directly store the content, not the address. This is different from the new object in java.
The addresses of p1.x and p1 are the same, indicating that p1 stores content, not an address.
The addresses of p1.x and p2.x are different, indicating that the memory space of p1 and p2 are different, and they are not the same object.

Some people will raise the bar and say that there is no object in Rust.
If I don’t learn deeply, I will inevitably make mistakes. Disassembly, I haven't read it yet, I don't rule out rust has made optimizations. If there is something wrong, please leave a message directly to express your opinion, I am happy to accept it.

The test code is as follows:

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

fn main() {
    
    
    let p1 = Point {
    
     x: 25, y: 25 };
    println!("p1.x address: {:p}", &(p1.x));
    println!("p1 address: {:p}", &p1);
    println!("p1.x和p1的地址相同,说明p1存的是内容,而不是地址。");
    println!("------------");
    let p2 = p1;
    println!("p2.x address: {:p}", &(p2.x));
    println!("p2 address: {:p}", &p2);
    println!("p1.x和p2.x的地址不同,说明p1和p2的内存空间不一样,不是同一个对象。");
}

The code running results are as follows:
Insert picture description here


comment

Guess you like

Origin blog.csdn.net/weixin_48502062/article/details/108803910