From 6c741e79ebccc9ee6dd9595f24c09b41e2b234ba Mon Sep 17 00:00:00 2001 From: Mike Vink Date: Thu, 14 Dec 2023 22:39:58 +0100 Subject: fromstr trait huh --- src/bin/day2.rs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) (limited to 'src') diff --git a/src/bin/day2.rs b/src/bin/day2.rs index a007159..8c5c54d 100644 --- a/src/bin/day2.rs +++ b/src/bin/day2.rs @@ -1,9 +1,21 @@ #![feature(test)] +use std::str::FromStr; use std::error::Error; const COLORS: [&str; 3] = ["red", "green", "blue"]; const AMOUNT: [u32; 3] = [12, 13, 14]; +struct HelloWorld { + msg: String, +} + +impl FromStr for HelloWorld { + type Err = std::convert::Infallible; + fn from_str(s: &str) -> Result { + Ok(HelloWorld { msg: String::from(s), }) + } +} + // NOTE: probably should just panic, filter_mapping doesn't make much sense if you need the input to be correct. fn main() -> Result<(), Box> { let file = std::fs::read_to_string("input/2/in.txt")?; @@ -61,6 +73,9 @@ fn main() -> Result<(), Box> { .sum(); dbg!(part2); + let v: HelloWorld = "hi".parse()?; + println!("msg from impl: {}", v.msg); + Ok(()) } -- cgit v1.2.3