-2

I have a pointer to an u64 value and I can't read it. I am getting this error:

error[E0507]: cannot move out of borrowed content
   --> /home/niko/sub/substrate/srml/system/src/lib.rs:533:32
    |
533 |             let mut aid: T::AccountId = *copy_who;
    |                                         ^^^^^^^^^
    |                                         |
    |                                         cannot move out of borrowed content
    |                                         help: consider removing the `*`: `copy_who`

How does one get around "borrowed content" error? What is the point of having a pointer to a variable if you can't read anything that it points to?

impl<T: Trait> Module<T> {
    // getter for AccountId
    pub fn get_account_id(who: &T::AccountId) -> T::AccountId {
        let mut copy_who: &T::AccountId = who;
        {
            let mut aid: T::AccountId = *copy_who;
            return aid;
        }
    }
}

AccountId is defined like this:

type AccountId = u64;
Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
Nulik
  • 6,748
  • 10
  • 60
  • 129
  • Please review how to create a [MCVE] and then [edit] your question to include it. We cannot tell what crates (and their versions), types, traits, fields, etc. are present in the code. Try to reproduce your error on the [Rust Playground](https://play.rust-lang.org) if possible, otherwise in a brand new Cargo project. There are [Rust-specific MCVE tips](//stackoverflow.com/tags/rust/info) you can use to reduce your original code for posting here. – Shepmaster May 15 '19 at 16:34
  • 3
    You have asked a number of poorly-received questions recently ([false warning “value assigned is never read”](https://stackoverflow.com/q/56122374/155423); [How do I print the the field of a struct when I get the error “no field on type std::result::Result”?](https://stackoverflow.com/q/56121904/155423)). Please consider re-evaluating your question-asking style based on the edits made to your questions and the requests that people have made. – Shepmaster May 15 '19 at 16:38
  • The code you have provided is not valid Rust: `Trait` and `Module` are not declared anywhere. Please **test** your code in a new Cargo project or in the Playground to ensure that the code you provide produces the error you are asking about. – Shepmaster May 15 '19 at 16:41
  • Btw, it's not a pointer, but a reference. It'a a huge difference. You should consider to read [the book](https://doc.rust-lang.org/stable/book/) to learn the language properly. – hellow May 15 '19 at 17:25

1 Answers1

2

Your problem can be reduced to

trait Example {
    type AccountId;
}

fn get_account_id<T>(who: &T::AccountId)
where
    T: Example,
{
    *who;
}
error[E0507]: cannot move out of borrowed content
 --> src/lib.rs:8:5
  |
8 |     *who;
  |     ^^^^ cannot move out of borrowed content

In order for this code to compile, T::AccountId must implement Copy:

fn get_account_id<T>(who: &T::AccountId)
where
    T: Example,
    T::AccountId: Copy,
{
    *who;
}

This is not the most flexible solution, however.

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366