@@ -490,42 +490,61 @@ hint = """
490
490
If other functions can return a `Result`, why shouldn't `main`?"""
491
491
492
492
[[exercises ]]
493
- name = " errorsn "
494
- path = " exercises/error_handling/errorsn .rs"
493
+ name = " errors4 "
494
+ path = " exercises/error_handling/errors4 .rs"
495
495
mode = " test"
496
496
hint = """
497
- First hint: To figure out what type should go where the ??? is, take a look
498
- at the test helper function `test_with_str`, since it returns whatever
499
- `read_and_validate` returns and `test_with_str` has its signature fully
500
- specified.
501
-
502
-
503
- Next hint: There are three places in `read_and_validate` that we call a
504
- function that returns a `Result` (that is, the functions might fail).
505
- Apply the `?` operator on those calls so that we return immediately from
506
- `read_and_validate` if those function calls fail.
497
+ `PositiveNonzeroInteger::new` is always creating a new instance and returning an `Ok` result.
498
+ It should be doing some checking, returning an `Err` result if those checks fail, and only
499
+ returning an `Ok` result if those checks determine that everything is... okay :)"""
507
500
501
+ [[exercises ]]
502
+ name = " errors5"
503
+ path = " exercises/error_handling/errors5.rs"
504
+ mode = " compile"
505
+ hint = """
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?
508
509
509
510
Another hint: under the hood, the `?` operator calls `From::from`
510
- on the error value to convert it to a boxed trait object, a Box<dyn error::Error>,
511
- which is polymorphic-- that means that lots of different kinds of errors
512
- can be returned from the same function because all errors act the same
513
- 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.
514
515
Check out this section of the book:
515
516
https://doc.rust-lang.org/book/ch09-02-recoverable-errors-with-result.html#a-shortcut-for-propagating-errors-the--operator
516
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
524
+
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
+ errors4.
517
536
518
- Another another hint: Note that because the `?` operator returns
519
- the *unwrapped* value in the `Ok` case, if we want to return a `Result` from
520
- `read_and_validate` for *its* success case, we'll have to rewrap a value
521
- that we got from the return value of a `?`ed call in an `Ok`-- this will
522
- look like `Ok(something)`.
537
+ Below the line that TODO asks you to change, there is an example of using
538
+ the `map_err()` method on a `Result` to transform one type of error into
539
+ another. Try using something similar on the `Result` from `parse()`. You
540
+ might use the `?` operator to return early from the function, or you might
541
+ use a `match` expression, or maybe there's another way!
523
542
543
+ You can create another function inside `impl ParsePosNonzeroError` to use
544
+ with `map_err()`.
524
545
525
- Another another another hint: `Result`s must be "used", that is, you'll
526
- get a warning if you don't handle a `Result` that you get in your
527
- function. Read more about that in the `std::result` module docs:
528
- https://doc.rust-lang.org/std/result/#results-must-be-used"""
546
+ Read more about `map_err()` in the `std::result` documentation:
547
+ https://doc.rust-lang.org/std/result/enum.Result.html#method.map_err"""
529
548
530
549
# Generics
531
550
@@ -561,7 +580,7 @@ ReportCard struct generic, but also the correct property - you will need to chan
561
580
of the struct slightly too...you can do it!
562
581
"""
563
582
564
- # OPTIONS / RESULTS
583
+ # OPTIONS
565
584
566
585
[[exercises ]]
567
586
name = " option1"
@@ -603,15 +622,6 @@ statement. How can this be avoided? The compiler shows the correction
603
622
needed. After making the correction as suggested by the compiler, do
604
623
read: https://doc.rust-lang.org/std/keyword.ref.html"""
605
624
606
- [[exercises ]]
607
- name = " result1"
608
- path = " exercises/error_handling/result1.rs"
609
- mode = " test"
610
- hint = """
611
- `PositiveNonzeroInteger::new` is always creating a new instance and returning an `Ok` result.
612
- It should be doing some checking, returning an `Err` result if those checks fail, and only
613
- returning an `Ok` result if those checks determine that everything is... okay :)"""
614
-
615
625
# TRAITS
616
626
617
627
[[exercises ]]
@@ -920,7 +930,7 @@ hint = """
920
930
Follow the steps provided right before the `TryFrom` implementation.
921
931
You can also use the example at https://doc.rust-lang.org/std/convert/trait.TryFrom.html
922
932
923
- You might want to look back at the exercise errorsn (or its hints) to remind
933
+ You might want to look back at the exercise errors5 (or its hints) to remind
924
934
yourself about how `Box<dyn Error>` works.
925
935
926
936
If you're trying to return a string as an error, note that neither `str`
0 commit comments