>>670 Rust 逆文字列を生成しない&整数ジェネリック版
use num::{BigInt, CheckedAdd, CheckedMul, CheckedSub, FromPrimitive};

fn chars_to_integer<X>(input: impl Iterator<Item = char>) -> Option<X>
 where X: FromPrimitive + CheckedMul + CheckedAdd,
{
 let (zero, ten) = (X::from_u32(0).unwrap(), X::from_u32(10).unwrap());
 input
  .map(|c| X::from_u32(c.to_digit(10)?))
  .try_fold(zero, |acc, x| acc.checked_mul(&ten)?.checked_add(&x?))
}

fn odai<X>(input: &str) -> Option<X>
 where X: FromPrimitive + CheckedMul + CheckedAdd + CheckedSub,
{
 let x = chars_to_integer::<X>(input.chars())?;
 let y = chars_to_integer::<X>(input.chars().rev())?;
 x.checked_sub(&y)
}

fn main() {
 assert_eq!(odai::<i64>("12345"), Some(-41976));
 assert_eq!(odai::<BigInt>("4922235242952026704037113243122008064"), Some("314233029528909399960910650696685770".parse::<BigInt>().unwrap()));
}