@@ -499,42 +499,49 @@ It should be doing some checking, returning an `Err` result if those checks fail
499
499
returning an `Ok` result if those checks determine that everything is... okay :)"""
500
500
501
501
[[exercises ]]
502
- name = " errorsn "
503
- path = " exercises/error_handling/errorsn .rs"
504
- mode = " test "
502
+ name = " errors5 "
503
+ path = " exercises/error_handling/errors5 .rs"
504
+ mode = " compile "
505
505
hint = """
506
- First hint: To figure out what type should go where the ??? is, take a look
507
- at the test helper function `test_with_str`, since it returns whatever
508
- `read_and_validate` returns and `test_with_str` has its signature fully
509
- specified.
510
-
511
-
512
- Next hint: There are three places in `read_and_validate` that we call a
513
- function that returns a `Result` (that is, the functions might fail).
514
- Apply the `?` operator on those calls so that we return immediately from
515
- `read_and_validate` if those function calls fail.
516
-
506
+ Hint: There are two different possible `Result` types produced within
507
+ `main()`, which are propagated using `?` operators. How do we declare a
508
+ return type from `main()` that allows both?
517
509
518
510
Another hint: under the hood, the `?` operator calls `From::from`
519
- on the error value to convert it to a boxed trait object, a Box<dyn error::Error>,
520
- which is polymorphic-- that means that lots of different kinds of errors
521
- can be returned from the same function because all errors act the same
522
- since they all implement the `error::Error` trait.
511
+ on the error value to convert it to a boxed trait object, a
512
+ `Box<dyn error::Error>`, which is polymorphic-- that means that lots of
513
+ different kinds of errors can be returned from the same function because
514
+ all errors act the same since they all implement the `error::Error` trait.
523
515
Check out this section of the book:
524
516
https://doc.rust-lang.org/book/ch09-02-recoverable-errors-with-result.html#a-shortcut-for-propagating-errors-the--operator
525
517
518
+ This exercise uses some concepts that we won't get to until later in the
519
+ course, like `Box` and the `From` trait. It's not important to understand
520
+ them in detail right now, but you can read ahead if you like.
521
+
522
+ Read more about boxing errors:
523
+ https://doc.rust-lang.org/stable/rust-by-example/error/multiple_error_types/boxing_errors.html
526
524
527
- Another another hint: Note that because the `?` operator returns
528
- the *unwrapped* value in the `Ok` case, if we want to return a `Result` from
529
- `read_and_validate` for *its* success case, we'll have to rewrap a value
530
- that we got from the return value of a `?`ed call in an `Ok`-- this will
531
- look like `Ok(something)`.
525
+ Read more about using the `?` operator with boxed errors:
526
+ https://doc.rust-lang.org/stable/rust-by-example/error/multiple_error_types/reenter_question_mark.html
527
+ """
528
+
529
+ [[exercises ]]
530
+ name = " errors6"
531
+ path = " exercises/error_handling/errors6.rs"
532
+ mode = " test"
533
+ hint = """
534
+ This exercise uses a completed version of `PositiveNonzeroInteger` from
535
+ the errors4.
532
536
537
+ Below the TODO line, there is an example of using the `.or()` method
538
+ on a `Result` to transform one type of error into another. Try using
539
+ something similar on the `Result` from `parse()`. You might use the `?`
540
+ operator to return early from the function, or you might use a `match`
541
+ expression, or maybe there's another way!
533
542
534
- Another another another hint: `Result`s must be "used", that is, you'll
535
- get a warning if you don't handle a `Result` that you get in your
536
- function. Read more about that in the `std::result` module docs:
537
- https://doc.rust-lang.org/std/result/#results-must-be-used"""
543
+ Read more about `.or()` in the `std::result` documentation:
544
+ https://doc.rust-lang.org/std/result/enum.Result.html#method.or"""
538
545
539
546
# Generics
540
547
0 commit comments