Open
Description
openedon Apr 18, 2024
What version of regex are you using?
1.10.4
Describe the bug at a high level.
When I run the quick example from the landing page of the regex docs, I get two errors.
What are the steps to reproduce the behavior?
First, cargo add regex
to a new project. Then, run the example from the docs. (I can't link to it directly, so I'll copy it below.)
use regex::Regex;
let re = Regex::new(r"(?m)^([^:]+):([0-9]+):(.+)$").unwrap();
let hay = "\
path/to/foo:54:Blue Harvest
path/to/bar:90:Something, Something, Something, Dark Side
path/to/baz:3:It's a Trap!
";
let mut results = vec![];
for (_, [path, lineno, line]) in re.captures_iter(hay).map(|c| c.extract()) {
results.push((path, lineno.parse::<u64>()?, line));
}
assert_eq!(results, vec![
("path/to/foo", 54, "Blue Harvest"),
("path/to/bar", 90, "Something, Something, Something, Dark Side"),
("path/to/baz", 3, "It's a Trap!"),
]);
What is the actual behavior?
I get an error:
error[E0277]: the `?` operator can only be used in a function that returns `Result` or `Option` (or another type that implements `FromResidual`)
--> src/main.rs:25:50
|
3 | fn main() {
| --------- this function should return `Result` or `Option` to accept `?`
...
25 | results.push((path, lineno.parse::<u64>()?, line));
| ^ cannot use the `?` operator in a function that returns `()`
|
= help: the trait `FromResidual<Result<Infallible, ParseIntError>>` is not implemented for `()`
If I change lineno.parse::<u64>()?
to lineno.parse::<u64>().unwrap()
the error goes away, but then I run into a different error:
thread 'main' panicked at src/main.rs:27:5:
assertion `left == right` failed
left: [("path/to/foo", 54, "Blue Harvest"), (" path/to/bar", 90, "Something, Something, Something, Dark Side"), (" path/to/baz", 3, "It's a Trap!")]
right: [("path/to/foo", 54, "Blue Harvest"), ("path/to/bar", 90, "Something, Something, Something, Dark Side"), ("path/to/baz", 3, "It's a Trap!")]
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
What is the expected behavior?
I expect the example to work without errors. I'm new to Rust so I may just be missing something, but I think my expectation is that I can essentially copy/paste the example from the docs and it should work.
Thank you for any insights on this!
Activity