Why not a `regex!` Wrapper for `Regex :: new` to offer the same regex matching speed?

The Rust Regex box offers a syntax extension regex!that allows you to compile a regular expression during standard compilation time. This is good in two ways:

  • we do not need to do this work at runtime (higher program performance)
  • If our regular expression is garbled, the compiler can tell us at compile time, rather than trigger a panic at runtime

Unfortunately, the docs say:

WARNING: The compiler plugin is an regex!order of magnitude slower than normal Regex::new(...)use. You should not use the compiler plugin unless you have specific reasons for this.

It seems that for regex!for regex!, only a different regular expression mechanism is used than for Regex::new(). Why not regex!()just a wrapper Regex::new()for combining the benefits of both worlds? As far as I understand, these plugins for extending extension syntax can execute arbitrary code; why not Regex::new()?

+4
source share
1 answer

The answer is very subtle: one feature of the macro is that the result regex!can be placed in static data , for example:

static r: Regex = regex!("t?rust");

, Regex::new() . Regex::new(), . burntsushi reddit.


, regex!:

  • static ,
  • static, , lazy_static!

2017 API 1.0. regex! , .

- , Regex::new(), : DFA , . , .

+6

Source: https://habr.com/ru/post/1665983/


All Articles