4

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.

1
  • Aside: do you really want a String here? Perhaps static STRING: &'static str = "Hello, world"; (no need for lazy_static) will suffice? Indeed, in that case, it could even be const. Commented May 26, 2021 at 14:56

1 Answer 1

5

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

fn main() {
    &STRING;
}

See also:

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.