I'm converting some Ruby/Crystal code to Rust and having problem with string<>number conversions. Here's is snippet with problem.
The Ruby version looks like this:
left_half = front_half.to_s
(palindrome = (left_half + left_half.reverse).to_i; basep = base11) if power.odd?
(palindrome = (left_half.chop + left_half.reverse).to_i; basep = base) if power.even?
The Crystal version looks like this:
palindrome, left_half = 0_u64, front_half.to_s
palindrome = (left_half + left_half.reverse).to_u64 if power.odd?
palindrome = (left_half.rchop + left_half.reverse).to_u64 if power.even?
basep = power.odd? ? base11 : base
The Rust version looks like this:
let (mut palindrome, left_half) = (0u64, this_lo.to_string());
if power & 1 == 1 { basep = base11; palindrome = (this_lo * base) + left_half.chars().rev().collect::<String>().parse::<u64>() }
else { basep = base; palindrome = (this_lo * 10) + left_half.chars().rev().collect::<String>().parse::<u64>() };
-
I couldn't find a simple way to do the equivalent of
chop|rchop
to remove the last char of a string; -
I get this error for the part that tries to reverse the string, convert it back to an integer, and add it to the preceding number.
palindrome = (this_lo * 10) + left_half.chars().rev().collect::<String>().parse::<u64>() };
| ^ no implementation for `u64 + std::result::Result<u64, ParseIntError>`
There's something I'm just not fundamentally understanding.