aboutsummaryrefslogtreecommitdiff
path: root/syn/examples/lazy-static/example/src
diff options
context:
space:
mode:
Diffstat (limited to 'syn/examples/lazy-static/example/src')
-rw-r--r--syn/examples/lazy-static/example/src/main.rs20
1 files changed, 20 insertions, 0 deletions
diff --git a/syn/examples/lazy-static/example/src/main.rs b/syn/examples/lazy-static/example/src/main.rs
new file mode 100644
index 0000000..c4f64af
--- /dev/null
+++ b/syn/examples/lazy-static/example/src/main.rs
@@ -0,0 +1,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));
+}