You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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 Harvestpath/to/bar:90:Something, Something, Something, Dark Sidepath/to/baz:3:It's a Trap!";letmut 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 functionthat returns `Result` or `Option` (or another type that implements `FromResidual`)
--> src/main.rs:25:50
|
3 | fn main() {
| --------- this functionshouldreturn`Result` or `Option` to accept `?`
...
25 | results.push((path, lineno.parse::<u64>()?, line));| ^ cannot use the `?` operator in a functionthat 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!
The text was updated successfully, but these errors were encountered:
The error tells you the problem. Your function should return a Result. The main function can do that.
I think I can add the main function to the example to avoid this specific speed bump. But doc examples generally have to assume some base background knowledge of Rust.
Thank you for explaining that @BurntSushi! There's definitely a lot I'm still absorbing about Rust :)
I think having the quickstart example be self-contained in a main function would be helpful for newcomers like me, but understand if it's not a Rust doc norm.
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.)What is the actual behavior?
I get an error:
If I change
lineno.parse::<u64>()?
tolineno.parse::<u64>().unwrap()
the error goes away, but then I run into a different error: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!
The text was updated successfully, but these errors were encountered: