mirror of
https://github.com/rust-lang/rustlings.git
synced 2024-11-18 21:43:53 +01:00
parent
ad03d180c9
commit
dcfb427b09
@ -1,8 +1,7 @@
|
||||
// errors3.rs
|
||||
// This is a program that is trying to use a completed version of the
|
||||
// `total_cost` function from the previous exercise. It's not working though--
|
||||
// we can't use the `?` operator in the `main()` function! Why not?
|
||||
// What should we do instead? Scroll for hints!
|
||||
// `total_cost` function from the previous exercise. It's not working though!
|
||||
// Why not? What should we do to fix it? Scroll for hints!
|
||||
|
||||
use std::num::ParseIntError;
|
||||
|
||||
@ -45,18 +44,4 @@ pub fn total_cost(item_quantity: &str) -> Result<i32, ParseIntError> {
|
||||
|
||||
|
||||
|
||||
// Since the `?` operator returns an `Err` early if the thing it's trying to
|
||||
// do fails, you can only use the `?` operator in functions that have a
|
||||
// `Result` as their return type.
|
||||
|
||||
// Hence the error that you get if you run this code is:
|
||||
|
||||
// ```
|
||||
// error[E0277]: the `?` operator can only be used in a function that returns `Result` (or another type that implements `std::ops::Try`)
|
||||
// ```
|
||||
|
||||
// So we have to use another way of handling a `Result` within `main`.
|
||||
|
||||
// Decide what we should do if `pretend_user_input` has a string value that does
|
||||
// not parse to an integer, and implement that instead of using the `?`
|
||||
// operator.
|
||||
// If other functions can return a `Result`, why shouldn't `main`?
|
||||
|
Loading…
Reference in New Issue
Block a user