What I really want to do is things like:
const FOO = NonZeroU8::new(env!("FOO").parse()).unwrap();
and of course the holy grail:
static PATTERN = Regex::new("patt[er]*n").unwrap();
The second of these require allocation in a static context, so that is very far from what can currently be done. The first can be done without allocation, but having a const parse
function would require a const FromStr
trait, which would make allocation impossible in other FromStr
impls. So that is kind of impossible to. Unless there was to be a separate const_parse
method and a separate ConstFromStr
trait. Which may or may not be worth having in std, but could start of in a separate crate.
Addendum: One such separate crate seem to be https://crates.io/crates/const_env (it does its "parsing" by just dumping the value in the source, so even expressions work, including use of other const values in the program, which may be awessome or ... "surprising" ... depending on how you look at it).