mirror of
https://github.com/rust-lang/rustlings
synced 2024-11-08 19:10:25 +00:00
b7ddd09fab
Adjust error text and naming to conform with best practices. Use `map_err()` instead of `or()`. Wrap lower-level errors instead of ignoring their details. Also, don't "cheat" by bypassing the `new()` function in tests. Fix a dangling reference in the try_from_into hints.
54 lines
1.4 KiB
Rust
54 lines
1.4 KiB
Rust
// errors5.rs
|
|
|
|
// This program uses a completed version of the code from errors4.
|
|
// It won't compile right now! Why?
|
|
// Execute `rustlings hint errors5` for hints!
|
|
|
|
// I AM NOT DONE
|
|
|
|
use std::error;
|
|
use std::fmt;
|
|
use std::num::ParseIntError;
|
|
|
|
// TODO: update the return type of `main()` to make this compile.
|
|
fn main() -> Result<(), ParseIntError> {
|
|
let pretend_user_input = "42";
|
|
let x: i64 = pretend_user_input.parse()?;
|
|
println!("output={:?}", PositiveNonzeroInteger::new(x)?);
|
|
Ok(())
|
|
}
|
|
|
|
// Don't change anything below this line.
|
|
|
|
#[derive(PartialEq, Debug)]
|
|
struct PositiveNonzeroInteger(u64);
|
|
|
|
#[derive(PartialEq, Debug)]
|
|
enum CreationError {
|
|
Negative,
|
|
Zero,
|
|
}
|
|
|
|
impl PositiveNonzeroInteger {
|
|
fn new(value: i64) -> Result<PositiveNonzeroInteger, CreationError> {
|
|
match value {
|
|
x if x < 0 => Err(CreationError::Negative),
|
|
x if x == 0 => Err(CreationError::Zero),
|
|
x => Ok(PositiveNonzeroInteger(x as u64))
|
|
}
|
|
}
|
|
}
|
|
|
|
// This is required so that `CreationError` can implement `error::Error`.
|
|
impl fmt::Display for CreationError {
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
let description = match *self {
|
|
CreationError::Negative => "number is negative",
|
|
CreationError::Zero => "number is zero",
|
|
};
|
|
f.write_str(description)
|
|
}
|
|
}
|
|
|
|
impl error::Error for CreationError {}
|