Moving Volatile Property

My understanding is that a volatile borrower can transfer ownership to another volatile borrower. But this move seems a bit different than moving a variable without a pointer. Let's see an example. Below p1moves to p2when compute()called for the first time. But upon return, compute()ownership returns to p1.

fn compute(p2: &mut Point) {
}

fn reset(p1: &mut Point) {
    compute(p1); //OK
    compute(p1); //OK
}

This is different from how a regular variable moves.

fn compute(p2:  Point) {
}

fn reset(p1: Point) {
    compute(p1); //OK
    compute(p1); //Compile error
}

Now, ownership does not return to p1after the first call returns compute().

Both behaviors are understandable and desirable for many reasons. But I just wanted to confirm my understanding that these two movements are somewhat different in nature. Do I think so?

+4
2

, , , , , . , , , .

+2

, , , , .

fn compute(p2:  Point) {
    // compute owns p2
} // owned p2 is freed

fn reset(p1: Point) {
    // reset() owns p1
    compute(p1); //Ownership of p1 is transferred to compute()
    compute(p1); //ERROR: p1 has already been freed
}

...

fn compute(p2:  &Point) {
    // compute is borrowing p2
} // borrowed p2 is given back to owner

fn reset(p1: Point) {
    // reset() owns p1
    compute(&p1); //let compute() borrow p1, then get it back
    compute(&p1); //let compute() re-borrow p1, then give it back
} // owned p1 is freed
+2

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


All Articles