rust


How to convert char to integer so that '1' becomes 1?


I am trying to find sum of all the digits of a given number, like 134 gives 8.
My plan is to iterate over the number by converting it into a string using .to_string() and then use .chars() to iterate over the number. Then I want to convert every char in the iteration into an integer and add into a variable. I want to get the final value of this variable.
I tried using the code below to convert a char into an integer (Playground):
fn main() {
let x = "123";
for y in x.chars() {
let z = y.parse::<i32>().unwrap();
println!("{}", z + 1);
}
}
But it results in this error:
error: no method named `parse` found for type `char` in the current scope
--> <anon>:4:19
|
4 | let z = y.parse::<i32>().unwrap();
| ^^^^^
How can I convert a char into an integer?
The below code does exactly what I want to do (Playground):
fn main() {
let mut sum = 0;
let x = 123;
let x = x.to_string();
for y in x.chars() {
// converting `y` to string and then to integer
let z = (y.to_string()).parse::<i32>().unwrap();
// incrementing `sum` by `z`
sum += z;
}
println!("{}", sum);
}
but first I have to convert char into a string and then into an integer to increment sum by z. Is there a way to directly convert char into integer?
The method you need is char::to_digit. It converts char to a number it represents in the given radix.
You can also use Iterator::sum to calculate sum of a sequence conveniently:
fn main() {
const RADIX: u32 = 10;
let x = "134";
println!("{}", x.chars().map(|c| c.to_digit(RADIX).unwrap()).sum::<u32>());
}
my_char as u32 - '0' as u32
Now, there's a lot more to unpack about this answer.
It works because the ASCII (and thus UTF-8) encodings have the Arabic numerals 0-9 ordered in ascending order. You can get the scalar values and subtract them.
However, what should it do for values outside this range? What happens if you provide 'p'? It returns 64. What about '.'? This will panic. And '♥' will return 9781.
Strings are not just bags of bytes. They are UTF-8 encoded and you cannot just ignore that fact. Every char can hold any Unicode scalar value.
That's why strings are the wrong abstraction for the problem.
From an efficiency perspective, allocating a string seems inefficient. Rosetta Code has an example of using an iterator which only does numeric operations:
struct DigitIter(usize, usize);
impl Iterator for DigitIter {
type Item = usize;
fn next(&mut self) -> Option<Self::Item> {
if self.0 == 0 {
None
} else {
let ret = self.0 % self.1;
self.0 /= self.1;
Some(ret)
}
}
}
fn main() {
println!("{}", DigitIter(1234, 10).sum::<usize>());
}
The method parse() is defined on str, not on char. A char is a Unicode codepoint, which is 32 bits wide. If you cast it to an integer, using u32 is preferred over i32.
You can cast it via as or into():
let a = '♥' as u32;
let b: u32 = '♥'.into();
Another way is to iterate over the characters of your string and convert and add them using fold.
fn sum_of_string(s: &str) -> u32 {
s.chars().fold(0, |acc, c| c.to_digit(10).unwrap_or(0) + acc)
}
fn main() {
let x = "123";
println!("{}", sum_of_string(x));
}

Related Links

impl PartialEq for an Enum so that multiple variants are equal
Destructure immutable reference and bind mutably in parameter list
Implementing Nested Traits
Is there a command to update cargo to the latest official release?
How to interleave stderr and stdin to a std::process::Command?
Why does my prime number sieve include the number 49?
How to implement a &mut iterator for a Binary Search Tree?
What does _ mean in type errors in Rust?
rust E0061: Some(A) is not the same as Some(match … =>A)
Class subtyping in Rust
Troubles deriving Show when updating very old code [duplicate]
Is it recommended to use traits to implement utility functions for structs in external crate?
Box type and method syntax in Rust [duplicate]
How to make mutable pointer to field of node of tree and mutate it?
Assigning *mut to a LocalKey
How to tell the Rust compiler that a borrow has ended?

Categories

HOME
xpath
flask
artificial-intelligence
lambda
xcode
focus
livecode
activex
jint
position
pyresttest
port
eddystone
wine
log4j2
migrate
alljoyn
parsley.js
erd
sap-fiori
graphdb
chromecast
smooks
android-externalstorage
mailmerge
orange-api
google-cloud-logging
fosuserbundle
cloudsim
nose
nullreferenceexception
bazaar
akka-persistence
coreldraw
stackexchange.redis
uislider
animated-gif
tracing
oracleforms
mdns
snap-framework
john-the-ripper
appcode
apptentive
cmis
restful-architecture
db2-luw
rails-postgresql
nanogallery
django-crispy-forms
export-to-pdf
data-uri
coverage.py
haar-classifier
httphandler
apache-toree
test-data
case-when
kendo-editor
swiperefreshlayout
petsc
mifos
scheduledexecutorservice
multi-touch
dpkg
wolfram-language
wicked-pdf
gwt-syncproxy
html.actionlink
swagger-maven-plugin
android-audiomanager
mod-auth-openidc
producer
inputbox
libgcc
full-text-indexing
treeline
prism.js
lync-2010
code39
spring-security-acl
notifyjs
mp4parser
ntlmv2
cyrillic
idispatch
border-box
thejit
image-scanner
monocross
flymake
microblogging
boost-foreach
aptitude
fgetc
zend-test
fuzzy-comparison
inversion
accumulator
motif
suggestbox

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App