I want to create a long &'static str
made of repeating sequences of chars, e.g. abcabcabc...
Is there a way in Rust to do this via an expression, e.g. something like long_str = 1000 * "abc"
in Python, or do I have to generate it in Python and copy/paste it in the Rust code?
You cannot do such a thing in stable Rust. Your example of
1000 * "abc"
is not run at "compile time" in Python either, as far as I understand Python.Including a file
If it has to be static, you could use a Cargo build script. This is a bit of Rust code that can do lots of things before your code is actually compiled. Specifically, you could write a source file out that has your string and then use
include_str!
to bring it into your crate:build.rs
lib.rs
Lazy initialization
You could create a lazy_static value that would have your string that would be created only once. This is done at runtime, but just once.
See also:
The far future
At some point, RFC 911 will be fully implemented. This, plus a handful of additional RFCs, each adding new functionality, will allow you to be able to write something like:
There are quite a few ways to do that. You could load a pre-generated string from file if you like:
Or to do it during compilation you can use
concat!
: