简体   繁体   中英

How do I avoid "cannot move out of static item" when using a lazy_static variable?

I want to initialize a variable globally, so I used a package called lazy_static .

lazy_static::lazy_static! {
    static ref STRING: String = String::from("Hello, World");
}

fn main() {
    STRING;
}
error[E0507]: cannot move out of static item `STRING`
 --> src/main.rs:6:5
  |
6 |     STRING;
  |     ^^^^^^ move occurs because `STRING` has type `STRING`, which does not implement the `Copy` trait

I don't want to implement the Copy trait, I want to use same reference all over the application, like a 'static lifetime.

Don't try to use the variable by value, use it by reference.

fn main() {
    &STRING;
}

See also:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM