I have several regular expressions that are defined at runtime and I would like to make them global variables.
To give you an idea, the following code works:
use regex::Regex; // 1.1.5
fn main() {
let RE = Regex::new(r"hello (w+)!").unwrap();
let text = "hello bob!
hello sue!
hello world!
";
for cap in RE.captures_iter(text) {
println!("your name is: {}", &cap[1]);
}
}
But I would like it to be something like this:
use regex::Regex; // 1.1.5
static RE: Regex = Regex::new(r"hello (w+)!").unwrap();
fn main() {
let text = "hello bob!
hello sue!
hello world!
";
for cap in RE.captures_iter(text) {
println!("your name is: {}", &cap[1]);
}
}
However, I get the following error:
error[E0015]: calls in statics are limited to constant functions, tuple structs and tuple variants
--> src/main.rs:3:20
|
3 | static RE: Regex = Regex::new(r"hello (w+)!").unwrap();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
Does this mean that I need nightly Rust in order to make these variables global, or is there another way to do it?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…