5

This code doesn't compile:

fn main() {
    let x = "".to_string();
    let y = &x;
    let z = *y;
}

Compiler error output is:

cannot move out of *y which is behind a shared reference

move occurs because *y has type String, which does not implement the Copy trait

I'm not very clear what's happening here and looking for an explanation.

I was expecting z taking ownership of the String and x and y becoming unusable.

3
  • 1
    Why do you expect z to take ownership of String ? You can borrow from an owned content but you cannot own an object from a borrowed content Commented Oct 26, 2022 at 9:30
  • @ÖmerErden The borrower y has done after *y and *y is x, so x has no borrower when its ownership is moved to z? But this doesn't seem to be how rust works. I guess (without proof) y is still in scope until the entire statement is finished.
    – TSK
    Commented Oct 26, 2022 at 9:35
  • *y is not exactly x, x is still there and owned by the current function's scope. *y, which is *&x, lets you access the value in the pointed memory location(&x). If you try to assign then it tries to move that value but the value's owner is x. If Rust lets you move that then what will happen to x? Commented Oct 26, 2022 at 10:22

2 Answers 2

3
let z = *y;

This line only knows y, and y is a &String. &String doesn't carry any information where it comes from, it only carries the information that it is a reference to a String. It doesn't know or care about the fact that x contains the actual content, nor does it have any control over x, apart of the fact that the borrow checker makes sure that x stays in scope and immutable.

So *y doesn't actually produce x, but an anonymous String value that is only accessible through a reference, meaning it can be used, but not owned.

By doing z = *y, you are attempting to own the value behind a reference. But as I said, this would require modifying x (as it isn't valid any more afterwards), and y has no power over x. So this isn't possible.

Because doing z = *y wouldn't be a problem with copyable types, as they don't require a transfer of ownership but simply get copied, Rust informs you that this isn't possible because the value that y references doesn't implement Copy.

5
  • 1
    I guess the key to understand this is *y is not the same as x. But doesn't y take the address of x as its own value on the stack? I always think about pointers in C/C++, but Rust has imposed its own ownership and borrow semantics?
    – TSK
    Commented Oct 26, 2022 at 11:16
  • 1
    Yes, it stores the pointer. But Rust isn't just typesafe like C/C++, it's also ownership safe, which is a concept that many new Rust programmers struggle with. So the compiler has to make sure that the address it stores is actually valid. There is also raw pointers in Rust which don't have any of those restrictions, but they require the unsafe keyword to be used.
    – Finomnis
    Commented Oct 26, 2022 at 11:19
  • 1
    @TSK The important concept to understand here is that Rust has zero undefined behaviour, because most attack surface of hackers/worms is based on undefined behaviour. That sounds simple, but has big implications on how the language is structured, like the borrowing rules you just stumbled over.
    – Finomnis
    Commented Oct 26, 2022 at 11:20
  • @TSK In theory you are right, the borrow checker could be made smart enough to understand that z = *y actually affects the x variable. It's just not there yet. Writing a borrow checker is hard work, and in your specific case, there's just no point in doing *y because you could simply do z = x. And in all other cases where you don't have easy access to the original variable x, you definitely don't want to allow people to move out of y.
    – Finomnis
    Commented Oct 26, 2022 at 11:28
  • 1
    For what it's worth, this "anonymous String value" is called a place. Commented Aug 29, 2023 at 13:27
-1

I have a different understanding. But my understanding may be incorrect. Through derefence operator(*y),an unnamed temporary variable is generated. This temporary variable differs from x(let x = "".to_string();) in that it does not take ownership. (let z = *y;) => moved out temporary variable, but not owner. So error.


https://rustwiki.org/en/reference/expressions.html#moved-and-copied-types

1
  • 1
    This seems to only differ from the accepted answer by terminology, but your terminology is not correct: what being created is not called a temporary but a place. Commented Aug 29, 2023 at 13:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.