Welcome to Software Development on Codidact!
Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.
Post History
using rfind and find as str.chars().find(|c| "0123456789".contains(c)) and str.chars().find(|c| "0123456789".contains(c)), then a manual match block is the simplest solution I think: edit str.find...
Answer
#3: Post edited
using [rfind](https://doc.rust-lang.org/std/primitive.str.html#method.rfind) and [find](https://doc.rust-lang.org/std/primitive.str.html#method.find) as `&str.find(|c| "0123456789".contains(c))` and `&str.find(|c| "0123456789".contains(c))`, then a manual match block is the simplest solution I think:- **edit** str.find returns index, so better to call `chars()` first for functional-style.
- ```rust
- fn char_to_int(c: char) -> u32 {
- match c {
- '0' => 0,
- '1' => 1,
- '2' => 2,
- '3' => 3,
- '4' => 4,
- '5' => 5,
- '6' => 6,
- '7' => 7,
- '8' => 8,
- '9' => 9,
- _ => u32::MAX, // make summing logic panic! for pleasure on invalid
- }
- }
- #[test]
- fn test_advent_2023() {
- let data = [
- "1abc2",
- "pqr3stu8vwx",
- "a1b2c3d4e5f",
- "treb7uchet",
- ];
- let mut sum: u32 = 0;
- for line in data {
- let first: u32 = line.chars().find(|c| "0123456789".contains(*c)).map(char_to_int).unwrap();
- let second: u32 = line.chars().rfind(|c| "0123456789".contains(*c)).map(char_to_int).unwrap();
- sum += first*10 + second;
- }
- println!("{sum:?}");
- assert_eq!(sum, 42) // make the test fail for output
- }
- ```
- using [rfind](https://doc.rust-lang.org/std/primitive.str.html#method.rfind) and [find](https://doc.rust-lang.org/std/primitive.str.html#method.find) as `str.chars().find(|c| "0123456789".contains(c))` and `str.chars().find(|c| "0123456789".contains(c))`, then a manual match block is the simplest solution I think:
- **edit** str.find returns index, so better to call `chars()` first for functional-style.
- ```rust
- fn char_to_int(c: char) -> u32 {
- match c {
- '0' => 0,
- '1' => 1,
- '2' => 2,
- '3' => 3,
- '4' => 4,
- '5' => 5,
- '6' => 6,
- '7' => 7,
- '8' => 8,
- '9' => 9,
- _ => u32::MAX, // make summing logic panic! for pleasure on invalid
- }
- }
- #[test]
- fn test_advent_2023() {
- let data = [
- "1abc2",
- "pqr3stu8vwx",
- "a1b2c3d4e5f",
- "treb7uchet",
- ];
- let mut sum: u32 = 0;
- for line in data {
- let first: u32 = line.chars().find(|c| "0123456789".contains(*c)).map(char_to_int).unwrap();
- let second: u32 = line.chars().rfind(|c| "0123456789".contains(*c)).map(char_to_int).unwrap();
- sum += first*10 + second;
- }
- println!("{sum:?}");
- assert_eq!(sum, 42) // make the test fail for output
- }
- ```
#2: Post edited
- using [rfind](https://doc.rust-lang.org/std/primitive.str.html#method.rfind) and [find](https://doc.rust-lang.org/std/primitive.str.html#method.find) as `&str.find(|c| "0123456789".contains(c))` and `&str.find(|c| "0123456789".contains(c))`, then a manual match block is the simplest solution I think:
- ```rust
use std::fs::read_to_string;use regex::Regex;fn char_to_int<T>(c: char) -> T {match c {'0' => 0,'1' => 1,'2' => 2,'3' => 3,'4' => 4,'5' => 5,'6' => 6,'7' => 7,'8' => 8,'9' => 9,_ => T::MAX, // make summing logic panic! for pleasure on invalid}}fn main() {let mut sum: u32 = 0;for line in read_to_string("calibration_data.txt").unwrap().lines() {let first: u32 = line.find(|c| "0123456789".contains(c)).map(char_to_int);let second: u32 = line.rfind(|c| "0123456789".contains(c)).map(char_to_int);sum += first*10 + second;}println!("{sum:?}");- ```
- using [rfind](https://doc.rust-lang.org/std/primitive.str.html#method.rfind) and [find](https://doc.rust-lang.org/std/primitive.str.html#method.find) as `&str.find(|c| "0123456789".contains(c))` and `&str.find(|c| "0123456789".contains(c))`, then a manual match block is the simplest solution I think:
- **edit** str.find returns index, so better to call `chars()` first for functional-style.
- ```rust
- fn char_to_int(c: char) -> u32 {
- match c {
- '0' => 0,
- '1' => 1,
- '2' => 2,
- '3' => 3,
- '4' => 4,
- '5' => 5,
- '6' => 6,
- '7' => 7,
- '8' => 8,
- '9' => 9,
- _ => u32::MAX, // make summing logic panic! for pleasure on invalid
- }
- }
- #[test]
- fn test_advent_2023() {
- let data = [
- "1abc2",
- "pqr3stu8vwx",
- "a1b2c3d4e5f",
- "treb7uchet",
- ];
- let mut sum: u32 = 0;
- for line in data {
- let first: u32 = line.chars().find(|c| "0123456789".contains(*c)).map(char_to_int).unwrap();
- let second: u32 = line.chars().rfind(|c| "0123456789".contains(*c)).map(char_to_int).unwrap();
- sum += first*10 + second;
- }
- println!("{sum:?}");
- assert_eq!(sum, 42) // make the test fail for output
- }
- ```
#1: Initial revision
using [rfind](https://doc.rust-lang.org/std/primitive.str.html#method.rfind) and [find](https://doc.rust-lang.org/std/primitive.str.html#method.find) as `&str.find(|c| "0123456789".contains(c))` and `&str.find(|c| "0123456789".contains(c))`, then a manual match block is the simplest solution I think: ```rust use std::fs::read_to_string; use regex::Regex; fn char_to_int<T>(c: char) -> T { match c { '0' => 0, '1' => 1, '2' => 2, '3' => 3, '4' => 4, '5' => 5, '6' => 6, '7' => 7, '8' => 8, '9' => 9, _ => T::MAX, // make summing logic panic! for pleasure on invalid } } fn main() { let mut sum: u32 = 0; for line in read_to_string("calibration_data.txt").unwrap().lines() { let first: u32 = line.find(|c| "0123456789".contains(c)).map(char_to_int); let second: u32 = line.rfind(|c| "0123456789".contains(c)).map(char_to_int); sum += first*10 + second; } println!("{sum:?}"); ```