I have a 'static str that I would like to verify meets a certain pattern at compile time using const fn. Is it possible to iterate of a str in a const way? The iterations are not const, and there doesn't seem to be a way to get the first char / tail of str via pattern matching.
It's possible to inspect a str
in a const
context, however ordinary iteration with Iterator
s and for
loops isn't possible, because Rust does not (yet) support any const
in trait methods.
One way you can work with str
is by the as_bytes
method; then, [u8]
can be indexed and thus effectively interested over (slice indexing is a built-in operation and thus does not involve traits and should be possible in const
IIRC).
1 Like
Thanks. I just found the konst crate and was able to use its constructors to do this. I suspect it's probably using as_bytes under the hood.