aboutsummaryrefslogtreecommitdiff
path: root/syn/examples/lazy-static/example
diff options
context:
space:
mode:
Diffstat (limited to 'syn/examples/lazy-static/example')
-rw-r--r--syn/examples/lazy-static/example/Cargo.toml10
-rw-r--r--syn/examples/lazy-static/example/src/main.rs20
2 files changed, 30 insertions, 0 deletions
diff --git a/syn/examples/lazy-static/example/Cargo.toml b/syn/examples/lazy-static/example/Cargo.toml
new file mode 100644
index 0000000..716b08c
--- /dev/null
+++ b/syn/examples/lazy-static/example/Cargo.toml
@@ -0,0 +1,10 @@
+[package]
+name = "example"
+version = "0.0.0"
+authors = ["David Tolnay <dtolnay@gmail.com>"]
+edition = "2018"
+publish = false
+
+[dependencies]
+lazy_static = { path = "../lazy-static" }
+regex = "0.2"
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));
+}