Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does `a` keeps the ownership after `let b = &*&a;` in Rust? [duplicate]

Tags:

rust

ownership

I found it confusing that &(*&a) and &{*&a} behave differently.

To be detailed, the following code failed to compile:

struct CanNotCopy;
fn main(){
    let a = CanNotCopy;
    &{*&a};
    let c = a;
}

And the following code compiled:

struct CanNotCopy;
fn main(){
    let a = CanNotCopy;
    &(*&a); // or &*&a;
    let c = a;
}

What is the semantical difference between the above codes?

Which are language constructs that lead to the transfer of ownership, precisely?

like image 568
yslking Avatar asked Mar 15 '21 08:03

yslking


People also ask

How do I remove occupier from HDB?

You can remove existing occupier/s if the remaining occupier/s qualify to retain the rental flat under our prevailing policy. You can submit your request by approaching the HDB Branch.

What happen to HDB when both owner pass away?

If a joint-owner has passed away, his or her share or interest in the flat will be transferred to the remaining owners, who will have to lodge a Notice of Death with the Singapore Land Authority (SLA).

Can I inherit my parents HDB if I own a private property?

Whether a private property owner can inherit an HDB or not depends on when the original owner purchased the HDB flat. If the HDB flat was purchased before 30th August 2010, then you can keep both the properties – the private residential property and the inherited HDB.

How can I check HDB ownership?

Flat owners can print the HDB's confirmation letter, after logging in with Singpass at http://www.hdb.gov.sg/rentingout, via Renting Out of Bedroom(s) > Enquiry on Tenants. Alternatively, flat owners may use our 'Enquiry on Authorised Tenants' e-Service to assure their tenants that they have been registered with HDB.


Video Answer


1 Answers

By using {} you are moving a into a new scope, which then you play referencing and dereferencing. It can also be simplified to:

struct CanNotCopy;
fn main(){
    let a = CanNotCopy;
    {a};
    let c = a;
}

You would be getting the same problem, now lets move to the other problem. Imagine you have a reference before the scope:

struct CanNotCopy;
fn main(){
    let a = CanNotCopy;
    let aa = &a;
    {aa};
    let c = a;
}

This works, because we just move the reference and not the original object itself.

How about the operations with * and & then:

struct CanNotCopy;
fn main(){
    let a = CanNotCopy;
    let aa = &a;
    &{*aa};
    let c = a;
}

We got to a cannot move out of *aa which is behind a shared reference because of the same reasons above showed.

We do not have that error with () because we are working within the same scope hence nothing is really moving there.

EDIT (taken from comments), Some related questions:

  • What does “&*” do in Rust

  • What's the difference between &mut unsafe { } and unsafe { &mut }?

like image 121
Netwave Avatar answered Sep 22 '22 09:09

Netwave