mirror of
https://github.com/rust-lang/rustlings.git
synced 2026-01-12 05:39:19 +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?
|
// Why not? What should we do to fix it?
|
||||||
// Execute `rustlings hint errors3` for hints!
|
// Execute `rustlings hint errors3` for hints!
|
||||||
|
|
||||||
// I AM NOT DONE
|
|
||||||
|
|
||||||
use std::num::ParseIntError;
|
use std::num::ParseIntError;
|
||||||
|
|
||||||
|
|||||||
@ -1,7 +1,6 @@
|
|||||||
// errors4.rs
|
// errors4.rs
|
||||||
// Make this test pass! Execute `rustlings hint errors4` for hints :)
|
// Make this test pass! Execute `rustlings hint errors4` for hints :)
|
||||||
|
|
||||||
// I AM NOT DONE
|
|
||||||
|
|
||||||
#[derive(PartialEq, Debug)]
|
#[derive(PartialEq, Debug)]
|
||||||
struct PositiveNonzeroInteger(u64);
|
struct PositiveNonzeroInteger(u64);
|
||||||
@ -14,9 +13,22 @@ enum CreationError {
|
|||||||
|
|
||||||
impl PositiveNonzeroInteger {
|
impl PositiveNonzeroInteger {
|
||||||
fn new(value: i64) -> Result<PositiveNonzeroInteger, CreationError> {
|
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))
|
Ok(PositiveNonzeroInteger(value as u64))
|
||||||
}
|
}
|
||||||
}
|
else if
|
||||||
|
value < 0 {
|
||||||
|
Err(CreationError::Negative)
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
Err(CreationError::Zero)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_creation() {
|
fn test_creation() {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user