mirror of
https://github.com/rust-lang/rustlings.git
synced 2026-01-11 13:19:18 +00:00
Error handling is tedious
This commit is contained in:
parent
d279e7e191
commit
14826e6232
@ -4,7 +4,6 @@
|
||||
// Why not? What should we do to fix it?
|
||||
// Execute `rustlings hint errors3` for hints!
|
||||
|
||||
// I AM NOT DONE
|
||||
|
||||
use std::num::ParseIntError;
|
||||
|
||||
|
||||
@ -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,9 +13,22 @@ enum CreationError {
|
||||
|
||||
impl PositiveNonzeroInteger {
|
||||
fn new(value: i64) -> Result<PositiveNonzeroInteger, CreationError> {
|
||||
/*
|
||||
The following works, but I think could be best solved with a match case, or enum?
|
||||
*/
|
||||
if value > 0 {
|
||||
Ok(PositiveNonzeroInteger(value as u64))
|
||||
}
|
||||
}
|
||||
else if
|
||||
value < 0 {
|
||||
Err(CreationError::Negative)
|
||||
}
|
||||
else {
|
||||
Err(CreationError::Zero)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#[test]
|
||||
fn test_creation() {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user