The [0..n] slicing is what I was surprised to not see being done. As for what you're suggesting, using from_utf8 adds a overhead to check that you're giving it valid UTF-8 (this does not get optimized away). Dipping into unsafe can help here:
But I would consider having INDENTS be a &str to get the cheap slicing without needing unsafe. Since putting in a string literal of 256 spaces is nasty I would use the const_format crate to generate the constant:
I'm not sure I could justify bringing in a package for a one-liner, just to avoid an "unsafe" which could be easily documented in a comment as actually safe.
As long as nothing is depending on 'static lifetime further down the line. (EDIT: ignore and see below)
> (or at least increase the upper limit to 16)
That should be doable for sure. Even something like (I'm not a rust developer, just have a passing familiarity, so my syntax may be off):
I think that would condense it all down into one line, rather than having special cases for all of 1 through 16.EDIT: after toying around in the rust playground, I think the following will support any indent level (within the bounds of the u8 type).
At the top level:
And then in the as_str function: