에러 5번 푸는중..

This commit is contained in:
blacktoast 2021-10-14 07:42:54 +00:00
parent b9022cfd1f
commit 8de108505d
2 changed files with 7 additions and 3 deletions

View File

@ -1,7 +1,6 @@
// errors4.rs
// Make this test pass! Execute `rustlings hint errors4` for hints :)
// I AM NOT DONE
#[derive(PartialEq, Debug)]
struct PositiveNonzeroInteger(u64);
@ -14,7 +13,12 @@ enum CreationError {
impl PositiveNonzeroInteger {
fn new(value: i64) -> Result<PositiveNonzeroInteger, CreationError> {
Ok(PositiveNonzeroInteger(value as u64))
match value{
(value) if value>0 =>Ok(PositiveNonzeroInteger(value as u64)),
(value) if value==0=>Err(CreationError::Zero),
(value) if value < 0=>Err(CreationError::Negative),
_=>Err(panic!("no"))
}
}
}

View File

@ -11,7 +11,7 @@ use std::fmt;
use std::num::ParseIntError;
// TODO: update the return type of `main()` to make this compile.
fn main() -> Result<(), ParseIntError> {
fn main() -> Result<(), (ParseIntError,CreationError)> {
let pretend_user_input = "42";
let x: i64 = pretend_user_input.parse()?;
println!("output={:?}", PositiveNonzeroInteger::new(x)?);