I would like to initialise some constants at the very top of my main.rs
file, like so:
const PRIVATE_KEY: Vec<u8> = std::fs::read("./jwtRS256.key").unwrap();
const PUBLIC_KEY: Vec<u8> = std::fs::read("./jwtRS256.pub.key").unwrap();
fn main () {
// do some stuff
}
However, I am getting a compiler error as follows:
error[E0015]: calls in constants are limited to constant functions, tuple structs and tuple variants
--> src/main.rs:16:30
|
16 | const PRIVATE_KEY: Vec<u8> = std::fs::read("./jwtRS256.key").unwrap();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
I understand; makes sense. The compiler wants to be able to evaluate the values of these constants at compile time and it can't. What is the alternative, though? I need those constants available all through my code, including in other modules. So, I cannot just move them as regular variables into the main
function. What do I do instead? (I am on Rust 1.47.)