Quick Thought: const{} blocks

One neat use case for this would be implementing a generic parse! macro. We already have FromStr for types that can be parsed from strings, but it’s impossible to use at compile-time leading to things like this crate.

With const blocks this macro could simply expand to

const { FromStr::from_str("some string").unwrap() }

Then I could write code like:

let x: u32 = parse!("123");
let y: String = parse!("hello");
let x: PublicKey = parse!("tgta61gja8w7099b17bc33sp49n1zawr037p1n4n7n19qkmgtf9g");
// etc.

And have the parsing (and any errors) happen at compile time.

1 Like