aboutsummaryrefslogtreecommitdiff
path: root/syn/examples/lazy-static/example/src/main.rs
blob: c4f64af907ae8592bc4e7b57356110384bf655f9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
use lazy_static::lazy_static;
use regex::Regex;

lazy_static! {
    static ref USERNAME: Regex = {
        println!("Compiling username regex...");
        Regex::new("^[a-z0-9_-]{3,16}$").unwrap()
    };
}

fn main() {
    println!("Let's validate some usernames.");
    validate("fergie");
    validate("will.i.am");
}

fn validate(name: &str) {
    // The USERNAME regex is compiled lazily the first time its value is accessed.
    println!("is_match({:?}): {}", name, USERNAME.is_match(name));
}