从对特征关联类型的引用中读取值时,如何解决"borrowed content"错误?



我有一个指向u64值的指针,我看不懂它。我遇到了这个错误:

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`

一个人如何解决"借入内容"错误?如果您无法读取任何指向变量的指针,请有什么意义?

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是这样定义的:

type AccountId = u64;

您的问题可以减少到

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

为了使此代码编译,T::AccountId必须实现Copy

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

这不是最灵活的解决方案。

  • 从原始类型参考复制的惯用方法是什么?
  • 如何在关联类型上定义特质界限?
  • 试图转移所有权时,不能搬出借来的内容
  • 不能搬出借来的内容
  • 什么是什么,"无法摆脱索引内容"。卑鄙?
  • "无法移出借来的内容"从结构字段分配变量

最新更新