Const fn Make array of size &str.len()

pub const fn create_kmp(s: &str) -> [u8; ?] {
    let mut lps = [0u8; s.len()]; //attempt to use a non-constant value in a constant
}

i am trying to return a mut array of size s.len() without allocating

The length of a &str is a dynamic (runtime) quality, whereas the types (including the length of arrays) have to be known at compile time.

1 Like

is there any way to convert like this

let j = b"abc"; //&[u8]
let whatiwant = "abc"; //[u8;3];

In that example j has type &[u8; 3], not &[u8]. To get a [u8; 3] you can just dereference it, i.e. *b"abc"

2 Likes

There's arrayvec that can support strings copied by value, up to a fixed size.

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.