8

Is there a way I can create a static String in Rust?

I tried this:

static somestring: String = String::new();

but I got this error:

error: `std::string::String::new` is not yet stable as a const fn
 --> src/main.rs:2:29
  |
2 | static somestring: String = String::new();
  |

How to create a static string at compile time does not solve my problem because it's about &'static str, not String. I need the String to be globally addressable.

LJ Germain
  • 467
  • 1
  • 6
  • 14
  • 1
    Considering that it is `static`, you can just make it a static string slice (`&'static str`), rather than a heap allocated `String`. See also https://stackoverflow.com/q/32956050/1233251 and https://stackoverflow.com/q/27791532/1233251 – E_net4 May 03 '19 at 21:09
  • 3
    There may be a misunderstanding of what a `static String` is (or would be). A `static` is something that is globally addressable (aka can be seen everywhere, including by objects from other libraries). A `'static` (notice the `'`) denotes a lifetimes that can span the lifetime of the entire program; any `static` has a lifetime `'static`. A `String` is, by definition, a heap-allocated structure that holds a `str` but can also extend in size (through reallocation). A `static String` therefor does not make much sense. You may want a `'static String`, see the other comment for that – user2722968 May 03 '19 at 21:27
  • 3
    As a comment to the original question, it feels harsh to mark this question as a duplicate of the singleton pattern. Two question with the same answer are not the same question. Specially in this case when it comes down to strings, which are owned and mutable, and have a different treatment than &'static str. – Josep May 05 '19 at 14:51

1 Answers1

7

Don't confuse the String type with the str type.

What are the differences between Rust's `String` and `str`?

A String is mutable and always heap-allocated.

A str, usually presented as &str is not mutable and is simply a view into a string.

Your question seems to confuse the idea of static and globally adressable. You may refer to (&'static str) as a string with a 'static lifetime. Strings with 'static lifetimes are common because they (mostly) represent hardcoded strings in the program. A 'static lifetime means the object (in this case, a string) will live until the end of the program. Most 'static objects are known at compile time. This should be natural to think about, because hardcoded strings are known at compile time. These strings can not mutate.

Strings on the other hand, are mutable. And you can make a String from a &'static str.

Given the lack of context in the question, if what you want is a String that is globally adressable and want to define it static I may suggest the macro lazy-static:

https://crates.io/crates/lazy_static

As sugested by mcarton, this question seems to boil down to the singleton pattern. You can learn more about its implementation in Rust here: How do I create a global, mutable singleton?

Josep
  • 676
  • 2
  • 8
  • 14