escape into a formatter
Currently, escape allocates a new string for its output. It's often the case that you want to use the output of escape in a larger string to specify the regex. It would be more convenient if the escaping method implements Display so it can be used directly in a format string without allocation. To get an allocated string, one can then always use the to_string method.
Suggested API:
struct Escape { ... }
impl Escape {
fn new(text: &str) -> Self { ... }
}
impl Display for Escape { ... }
I guess in principle I agree, but can you actually come up with a benchmark where this matters? I'd be surprised if you could. The benchmark should include regex compilation. My instinct is that these sorts of allocations would be dwarfed by the compilation process itself.
Alternatively, it could return an iterator like std library does for EscapeDefault and EscapeDebug, then you can either .collect() to String or .extend existing String (and it will be more consistent with std).
If someone can give me a good use case for avoiding allocation for something like this, then I'd be happy to revisit an API like this. But otherwise I don't think it's worth doing.