From 6fe087abb211ef550a2601ab19cbcfc6a3eebf7d Mon Sep 17 00:00:00 2001 From: Dory Date: Tue, 12 Mar 2024 09:21:21 -0700 Subject: [PATCH] d10p2 trying question mark --- day10/src/main.rs | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/day10/src/main.rs b/day10/src/main.rs index ef65e00..eca3d1f 100644 --- a/day10/src/main.rs +++ b/day10/src/main.rs @@ -1,3 +1,4 @@ +use std::num; use std::env; use std::process; use std::fs::File; @@ -11,14 +12,16 @@ enum Inst{ #[derive(Debug)] struct ParseError(String); +impl From for ParseError { + fn from(e: num::ParseIntError) -> Self { + ParseError(e.to_string()) + } +} fn parse_line(line: String) -> Result { match &line[..4] { "noop" => Ok(Inst::Noop), - "addx" => match line[5..].parse::() { - Ok(operand) => Ok(Inst::AddX(operand)), - Err(e) => Err(ParseError(e.to_string())), - }, + "addx" => Ok(Inst::AddX(line[5..].parse::()?)), _ => Err(ParseError(String::from("bad instruction"))), } }