How can I compile this?

fn main() {
    const s: &str = "⊗ symbol";
    const n: usize =  s.chars().count();
    let mut a: [char; n];

Have you copied the code to a text file and pointed rustc at it?

For example, say the code is in

$ rustc

At this time, I'd suggest just using a Vec<char> rather than stack-allocating a fixed sized array. Something like

fn main() {
    const s: &str = "⊗ symbol"
    let n = s.chars().count();
    let mut a = vec![char; n];

If you really need n as a const, there are ways of doing that, but it'll be significantly more complicated than just using a vec, and in 99% of cases I would say it's not worth it.

It will be possible to easily count a string's characters in future versions of rust, but const evaluation is not a complete feature today, and cannot do all general computation.

1 Like

This is a duplicate of these two other topics. Please don't spam this forum with multiple threads on the same subject.