rust REGEX和lazy_static 和struct 混用

lazy_static! {
    static ref SSID_REG: Regex = Regex::new(r"^[0-9a-zA-Z-_]{1,32}$").unwrap();
}

#[derive(Serialize, Deserialize, Validate, Debug, Clone)]
pub struct WifiClient {
    pub enabled: bool,
    #[validate(regex = "SSID_REG")]
    pub ssid: String,
    pub freq: ChannelConnect,
}
  • Lazy(通常来自 once_cell::sync::Lazylazy_static)保证这个 Regex 对象只会在第一次被使用时构建一次,之后所有线程都能安全地共享它。

  • 相比每次调用 Regex::new(...) 都重新编译,这样可以大幅提升性能。

你可能感兴趣的:(java,前端,javascript)