rustlings icon indicating copy to clipboard operation
rustlings copied to clipboard

how can i fix the question of errors6

Open tmdhhl opened this issue 2 years ago • 9 comments

I have no idea on how to solve this problem, can anyone help me? Thanks.

tmdhhl avatar Jul 25 '21 03:07 tmdhhl

Hi @tmdhhl

The Issues are usually for reporting issue in rustlings. If you could elaborate on where you are stuck at then we could try and add more details to the problem description or hint.

You could use rustlings hint errors6 to get hint or help and follow the documents or suggestions provided there.

Solutions to rustlings are available in Github by those who have solved, you could take a look at how it is solved. Here is my solution to the problem statement: https://github.com/suryapandian/rustlings/blob/main/exercises/error_handling/errors6.rs

While posting doubts I would recommend you to post the doubt with more details like the solution that you have tried, the error you received etc... so that more people would be able to help.

Here are some resources you could like into:

Newbie references: https://www.rust-lang.org/learn https://doc.rust-lang.org/book https://github.com/rust-lang/rustlings/ https://tourofrust.com/ https://doc.rust-lang.org/stable/rust-by-example/

Books: https://lborb.github.io/book/ http://web.mit.edu/rust-lang_v1.25/arch/amd64_ubuntu1404/share/doc/rust/html/book/first-edition/README.html

Discussion forums https://www.rust-lang.org/community https://rust-lang.zulipchat.com https://users.rust-lang.org/ https://discord.com/invite/aVESxV8 https://rust-slack.herokuapp.com/

We can close this issue since this does not have details or suggestions. If you would like to contribute to rustlings, please feel to add changes and reopen the issue or rise a new issue.

Happy coding!

Thanks

suryapandian avatar Jul 25 '21 06:07 suryapandian

The best documentation on map_err I've found is at https://docs1.w3cub.com/rust/book/error-handling/

ghost avatar Aug 01 '21 19:08 ghost

The current Version 43d0623 contains:

impl ParsePosNonzeroError {
    // TODO: add another error conversion function here.
}

Which suggest there allready is a error conversion function. I had a very hard time finding any hints about converting errors. It's only with this solution https://lazyren.github.io/studylog/rustlings-error-handling.html#errors6rs I found that maybe there was a first defiinition of a function in that impl. So maybe this functoin should read:

impl ParsePosNonzeroError {
    // TODO: add another error conversion function here.
    fn from_parse_int(err: ParseIntError) -> ParsePosNonzeroError {
        ParsePosNonzeroError::ParseInt(err)
    }
}

? I've searched around the web how map_err(...) is generally used. Passing a function seems to be pretty uncommon these days. https://docs1.w3cub.com/rust/book/error-handling/ doesn't contain such an example.

PJaros avatar Jul 07 '22 05:07 PJaros

For another solution, you might use match keyword, and does not need to create more function to handle error

    // TODO: change this to return an appropriate error instead of panicking
    // when `parse()` returns an error.
    let x: i64 = match s.parse::<i64>() {
        Err(err) => return Err(ParsePosNonzeroError::ParseInt(err)),
        Ok(n) => n
    };

dzungtran avatar Jul 18 '22 08:07 dzungtran

This worked for me!

For another solution, you might use match keyword, and does not need to create more function to handle error

    // TODO: change this to return an appropriate error instead of panicking
    // when `parse()` returns an error.
    let x: i64 = match s.parse::<i64>() {
        Err(err) => return Err(ParsePosNonzeroError::ParseInt(err)),
        Ok(n) => n
    };

This worked for me. Thankyou!

jbloomAus avatar Aug 16 '22 08:08 jbloomAus

As another newbie rustlings learner, I had a hard time with this as well. May I suggest that developers consider an "I give up, show me" command (with some explanation of why solution works) and/or a second level hint. rustlings list could show where this command was used. I realize this would be a lot of work for volunteer developers so it might not ever happen.

I just figured out errors6 (my solution is a bit different than above although that helped), but it took me a couple of days (not spending entire days on this, of course; I'm not insane :-) My point is that I read various sources to try to understand this and I am not simply looking for an easy shortcut. (I did not try discussion forums, but that is an excellent suggestion from @suryapandian, and probably would have been my next step. Sometimes newbies just get stuck, and ultimately the goal is to learn.

Steve-Z avatar Nov 11 '22 17:11 Steve-Z

Complete the TODO area by creating a from_parseint function:

impl ParsePosNonzeroError {
    fn from_creation(err: CreationError) -> ParsePosNonzeroError {
        ParsePosNonzeroError::Creation(err)
    }
    fn from_parseint(err: ParseIntError) -> ParsePosNonzeroError {
        ParsePosNonzeroError::ParseInt(err)
    }
}

And the parse_pos_nonzero part you can use map_err() to indicate the function or use it with a closure:

fn parse_pos_nonzero(s: &str) -> Result<PositiveNonzeroInteger,ParsePosNonzeroError> {
    let x: i64 = s.parse().map_err(ParsePosNonzeroError::from_parseint)?; // pass the function without calling it
    Ok(PositiveNonzeroInteger::new(x).map_err(|err| ParsePosNonzeroError::from_creation(err))?) // or invoke a closure to call the function
}

junio256 avatar May 20 '23 21:05 junio256