How to concat [&str; N] in compile time?

I only know concat! but it only allows string literals. if I have a list like ["a", "b", "c"], and I need to const S = "a+b+c+" from this list, what can I do?

Would const_format::concatcp work for you?

2 Likes

My actual list is of 21 length. This seems laborious :open_mouth:

Sorry, this might be meaningless because build.rs and runtime concat seem better to resolve this. :pouting_cat:

1 Like

Do you need something simple like my_concat!("a", "b", "c"), or do you need to be able to pass other constants like in the example below?

const ARRAY: [&str; 3] = ["a", "b", "c"];
const S: &str = my_concat!(ARRAY);
assert_eq!(S, "a+b+c+");

In the first case, it's only the matter of creating a recursive declarative macro, but I don't think it's possible in the second case because you'd need to access another variable's content at compile-time.

1 Like

interesting implementation

Well, that's what I hinted at, but since I wasn't sure of the use-case, I asked before proposing a solution. So I suppose it was the first case.

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.