How do global consts that are not copy or clone work in Rust?

二次信任 提交于 2020-07-08 03:55:10

问题


Say I have the following snippet (playground)

struct A {
    pub val: u32
}

const GLOBAL_A: A = A {val: 2};

fn main() {
    let some_a: A = GLOBAL_A;
    let other_a: A = GLOBAL_A;

    println!("double val = {}", some_a.val + other_a.val);
}

Since A is neither Clone nor Copy, I would assume the value of GLOBAL_A would be moved. That does not make much sense for a const and as shown cannot be the case anyways since it can be "moved" twice.

What are the rules that allow the above snippet to work considering A is not Clone nor Copy?


回答1:


Constants are always inlined. Your example is essentially the same as

struct A {
    pub val: u32
}

fn main() {
    let some_a: A = A {val: 2};
    let other_a: A = A {val: 2};

    println!("double val = {}", some_a.val + other_a.val);
}

The value is reconstructed twice, so it doesn't need to be Copy or Clone.

On the other hand, statics are not inlined:

struct A {
    pub val: u32
}

static GLOBAL_A: A = A {val: 2};

fn main() {
    let some_a: A = GLOBAL_A;
}

results in

error[E0507]: cannot move out of static item `GLOBAL_A`
 --> src/main.rs:8:21
  |
8 |     let some_a: A = GLOBAL_A;
  |                     ^^^^^^^^
  |                     |
  |                     move occurs because `GLOBAL_A` has type `A`, which does not implement the `Copy` trait
  |                     help: consider borrowing here: `&GLOBAL_A`


来源:https://stackoverflow.com/questions/59515419/how-do-global-consts-that-are-not-copy-or-clone-work-in-rust

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!