This tutorial will go through how to convert a string to an integer/float and vice versa.
Table of contents
Numbers in Rust
Integers
Signed integer types start with i
and unsigned integer types start with u
. Signed and unsigned refer to whether the number can be negative. Integers types indicate the amount of space the number takes up in bits. For example, u32
is an unsigned integer that takes up 32 bits of space.
Floats
Rust has two primitive types for floating-point numbers, which are numbers with decimal points. The floating-point types are f32
, and f64
are 32 and 64 bits in size. The default type for modern CPUs is f64
, as it provides more precision for the same speed as f32
.
Rust Convert String to Integer
We can directly convert a string to an int using the str::parse::<T>()
method. The parse()
method needs to know what type to convert to. We can either specify the type to parse with the turbofish operator (::<>
) or via explicit type annotation. In the following examples, we will convert a string to an i32
integer:
fn main() { let str = "456"; // Convert to integer specifying type with turbofish operator let num = str.parse::<i32>().unwrap(); println!("{}", num); }
456
fn main() { let str = "456"; // Convert to integer explicitly specifying type let num: i32 = str.parse().unwrap(); println!("{}", num); }
456
Rust Convert Integer to String
We can convert an integer to a string using the to_string()
method as follows:
fn main() { let num = 456; let str = num.to_string(); println!("{}", str); }
456
We can also use the format!
macro as follows:
fn main() { let num = 456; let str = format!("{}", num); println!("{}", str); }
456
Rust Convert String to Float
To convert a string to a float in Rust, we can use the parse()
method and specify f64
as the floating-point number type. Let’s look at an example:
fn main() { let e = "2.71828"; let num: f64 = e.parse().unwrap(); println!("{}", num); }
2.71828
Rust Convert Float to String
We can convert a floating-point number to a string using the to_string()
function.
fn main() { let e = 2.71828; let str = e.to_string(); println!("{}", str); }
2.71828
We can also use the format!
macro to convert a float to a string. The format!
macro is handy because we can specify the number of decimal places to round the floating-point number. Let’s look at an example:
fn main() { let pi = 3.1415926; // Specify number of decimal places in format string let str = format!("{:.2}", pi); println!("{}", str); }
3.14
Rust Convert Char to Integer
To convert a single char to an integer in Rust, we can use .to_digit(radix)
. The radix
is also called a base. A radix of two indicates a binary number, a radix of ten is decimal, and a radix of sixteen is hexadecimal. The method to_digit() method also supports arbitrary radices. Let’s look at an example of converting a character to a decimal.
fn main() { let ch = '4'; let num = ch.to_digit(10).unwrap(); println!("{}", num); }
2
Let’s look at an example of converting a character to hexadecimal:
fn main() { let ch = 'b'; let num = ch.to_digit(16).unwrap(); println!("{}", num); }
11
Rust Convert Integer to Char
We can convert an integer to a char using the char::from_digit(radix)
method. We need to pass the radix
to the method, and the number must be u32
. Let’s look at an example:
fn main() { let num = 4; let str = char::from_digit(num, 10).unwrap(); println!("{}", str); }
4
Summary
Congratulations on reading to the end of this tutorial! We have gone through how to convert a string to an integer or a float and vice-versa.
For further reading on Rust, go to the articles:
Have fun and happy researching!
Suf is a senior advisor in data science with deep expertise in Natural Language Processing, Complex Networks, and Anomaly Detection. Formerly a postdoctoral research fellow, he applied advanced physics techniques to tackle real-world, data-heavy industry challenges. Before that, he was a particle physicist at the ATLAS Experiment of the Large Hadron Collider. Now, he’s focused on bringing more fun and curiosity to the world of science and research online.