38

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?

Chayim Friedman
  • 47,971
  • 5
  • 48
  • 77
mhristache
  • 2,038
  • 3
  • 20
  • 19

4 Answers4

32

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

use std::{
    env, error::Error, fs::File, io::{BufWriter, Write}, path::Path,
};

fn main() -> Result<(), Box<Error>> {
    let out_dir = env::var("OUT_DIR")?;
    let dest_path = Path::new(&out_dir).join("long_string.txt");
    let mut f = BufWriter::new(File::create(&dest_path)?);

    let long_string = "abc".repeat(100);
    write!(f, "{}", long_string)?;

    Ok(())
}

lib.rs

static LONG_STRING: &'static str = include_str!(concat!(env!("OUT_DIR"), "/long_string.txt"));

Lazy initialization

You could create a once_cell or lazy_static value that would create your string at runtime, but only once.

use once_cell::sync::Lazy; // 1.5.2

static LONG_STR: Lazy<String> = Lazy::new(|| "abc".repeat(5000));

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:

// Does not work yet!
static LONG_STR: String = "abc".repeat(1000);
Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
  • 2
    There is [`str::repeat()`](https://doc.rust-lang.org/nightly/std/primitive.str.html#method.repeat) now, so you can write `"abc".repeat(100)` to generate a `String` with your repeated data. A bit cleaner than the `iter::repeat()` version :) – Lukas Kalbertodt Sep 28 '17 at 15:03
17

There are quite a few ways to do that. You could load a pre-generated string from file if you like:

const DATA: &'static str = include_str!("filename.txt");

Or to do it during compilation you can use concat!:

const DATA: &'static str = concat!("abc", "abc");
Lukas Kalbertodt
  • 79,749
  • 26
  • 255
  • 305
6

Not proud of this answer :D but I wanted to to give a different perspective.

By using macro-rules, you can easily define static concatenation by composition. In this case, I define 100 * str = 4 * 25 * str = 4 * 5 * 5 * str. You could also do 100 * str = 10 * 10 * str, in less lines (but more columns :))

macro_rules! rep {
    ($t:expr, 4) => { concat!($t, $t, $t, $t) };
    ($t:expr, 5) => { concat!($t, $t, $t, $t, $t) };
    ($t:expr, 25) => { rep!(rep!($t, 5), 5) };
    ($t:expr, 100) => { rep!(rep!($t, 25), 4) };
}


fn main() {
    assert_eq!(rep!("x", 100).len(), 100);
}

Since macros work on language elements, it's not possible to use a counter and simple recursively call the macro like this:

macro_rules! does_not_work {
    ($t:expr, 1) => { $t };
    ($t:expr, $n:) => { concat!($t, does_not_work!($t, $n-1)) };
}

But recursively composing the macro should do the trick in this simple case. I didn't try using different macro_rules patterns or other kind of macros, but it should be possible to do something more elegant.

AkiRoss
  • 11,745
  • 6
  • 59
  • 86
2

There are crates that do that, e.g. const_format::str_repeat!().

Chayim Friedman
  • 47,971
  • 5
  • 48
  • 77