Skip to content
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

Errors when running quickstart from docs #1183

Open
bertday opened this issue Apr 18, 2024 · 2 comments
Open

Errors when running quickstart from docs #1183

bertday opened this issue Apr 18, 2024 · 2 comments
Labels

Comments

@bertday
Copy link

bertday commented 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!

@BurntSushi
Copy link
Member

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.

@BurntSushi BurntSushi added the doc label Apr 18, 2024
@bertday
Copy link
Author

bertday commented Apr 19, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants