-
Notifications
You must be signed in to change notification settings - Fork 443
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
0.2 Syntax regarding .at() and .get() got a little less usable #338
Comments
I'm not sure what to do about this. What do you think the alternative is? Adding more methods doesn't seem like a good idea, since that increases the number of choices a newcomer is faced with, which can be daunting. Did you look at the documentation while writing code? Could you say something about why that didn't help you? In particular, take a look at the examples for let msg = caps[2];
The standard answer here is "this is what unwrap is for." But in this case, you can access captures by index. e.g.,
But not always. The API must provide a way to differentiate between "no match" and "empty string match."
The key change was the addition of the |
OK, after reading IRC and reflecting more, I see that you considered the let msg = caps.get(2).map(|m| m.as_str()).unwrap_or("");
let msg = caps.get(2).map_or("", |m| m.as_str()); Doing the eta reduction in this case tends to make it a bit more verbose because of the |
Thanks for the quick answer. I hadn't thought about "no match" vs "empty string match", correct. Maybe I'm biased as I hadn't known I think adding this TLDR: the use in 0.1 seemed very convenient while I think in 0.2 this is less nice of a shorthand. Nothing more. |
Improving the docs is something I can get on board with. In general, I think I'm hesitant to add new methods that conveniences over a simple combinator or two, and would prefer to solve those issues for newcomers with better docs. However, I'd be willing to bend on that if there was compelling evidence that it was a big stumbling block for folks. |
I have a doc fix incoming for this. @winks I think it's worth pointing out that in some number of cases, using indexing, e.g., |
The example shows how to get the matched text of any capture group while defaulting to the empty string if that particular group didn't participate in the match. Fixes rust-lang#338
Those comments look awesome, I can't wish for more :) 👍 For completeness, this was my main example (sending let event_cmd_quit = format!("!quit(\\s+)?(.*)?");
let re_cmd_quit = Regex::new(&event_cmd_quit[..]).unwrap();
let caps_cmd = re_cmd_quit.captures(msg).unwrap();
let quit_msg = caps_cmd.get(2).map_or("", |m| m.as_str()); it has two possible matches in the end, but just the non-captured part is fine on its own. Seeing it again I should've probably used '(\s+(.+))?' - but I guess with "optional capture group at the end" it's always the same edge case that Anyway, this can be closed from my point of view - and thanks for your time. |
Hi,
not a huge problem but I just stumbled over this while bumping "regex = 0.1" to "0.2:
I was using a lot of this (IRC parsing code):
Now it looks like this (thanks #rust for the help)
So my reasoning is now this:
So I don't really want to complain, but I think the syntax (for this use case) got less usable and more verbose, but I don't know if it's worth fixing or if .at() had any other problems.
The text was updated successfully, but these errors were encountered: