70 %
Chris Biscardi

lazy_static vs once_cell

The lazy_static crate offers a macro that lets you initialize some code at runtime.

rust
use ed25519_dalek::PublicKey;
use lazy_static::lazy_static;
use std::collections::HashMap;
use std::env;
lazy_static! {
static ref PUB_KEY: PublicKey = PublicKey::from_bytes(
&hex::decode(
env::var("DISCORD_PUBLIC_KEY")
.expect("Expected DISCORD_PUBLIC_KEY to be set in the environment")
)
.expect("Couldn't hex::decode the DISCORD_PUBLIC_KEY")
)
.expect("Couldn't create a PublicKey from DISCORD_PUBLIC_KEY bytes");
static ref DISCORD_BOT_TOKEN: String =
env::var("DISCORD_BOT_TOKEN").expect("Expected a DISCORD_BOT_TOKEN");
static ref ROLE_REVERSE_MAP: HashMap<&'static str, &'static str> = {
let mut map = HashMap::new();
map.insert("837304749312180285", "corgi");
map
};
}
fn main() {
println!(
"User asked for \"{}\" role.",
ROLE_REVERSE_MAP.get("837304749312180285").unwrap()
);
}