Adding the necessary dependencies

To use regular expressions in our server, we need two crates: regex and lazy_static. The first provides a Regex type to create and match regular expressions with strings. The second helps to store Regex instances in a static context. We can assign constant values to static variables, because they're created when a program loads to memory. To use complex expressions, we have to add an initialization code and use it to execute expressions, assigning the result to a static variable. The lazy_static crate contains a lazy_static! macro to do this job for us automatically. This macro creates a static variable, executes an expression, and assigns the evaluated value to that variable. We can also create a regular expression object for every request in a local context using a local variable, rather than a static one. However, this takes up runtime overhead, so it's better to create it in advance and reuse it.

Add both dependencies to the Cargo.toml file:

[dependencies]
slab = "0.4"
futures = "0.1"
hyper = "0.12"
lazy_static = "1.0"
regex = "1.0"

Add two imports, in addition to the imports in the main.rs source file from the previous example:

use lazy_static::lazy_static;
use regex::Regex;

We'll use the lazy_static macro and the Regex type to construct a regular expression.