Add method to get the full match of a Captures without an unwrap
ultrabear opened this issue · 2 comments
A Problem
The current design of regex::Captures has a single way of getting the full match that was captured, via Captures.get(0)
, this is doc guaranteed to always be Some(Match)
.
The plain issue is that users of the crate must either match on a case that will never happen, or more likely will just call unwrap because the docs said it was ok, this is not ideal as clippy and code reviewers both dislike uses of unwrap, so now you also need a comment explaining that yes its ok
I wrote this in my code today:
// docs said this unwrap is ok
let item = capture.get(0).unwrap();
A Solution
The simple approach to improve downstream users code legibility: providing a method that gets the full match for us
// potential name 1
Captures::as_match -> Match
// potential name 2
Captures::full -> Match
Implementations
There is one simple implementation route I can think of, and potentially a second special one depending on the layout of the regex internals:
- Just call .get(0) and unwrap internally, this is what users will likely do anyways, but now the liability has moved to the regex crate, not each user
- If the full match is stored separately in a typed manner internally, just grab that internal object and return it, no possible panic at all
Closing as dup of #1146 oops, I was under the impression this had not been opened yet due to a discussion in the community server
Yes, I forgot there was already an open issue. It's okay.