We can use the Rust functions to_string(), and format functions to format or convert integer values to string values. This post shows how to use these functions on each of Rust’s integer types.
Rust Integer Types
There are six (6) integer types in Rust. These are i8/ u8 , i16/ u16, i32/ u32 , i64/ u64 , and isize/ usize .
Convert i8 and u8 Integers to String
The integer types i8 and u8 are 8-bit signed and unsigned storage, respectively.
1 2 3 4 5 6 7 8 9 10 | let int_i8_val:i8 = 1; let str_from_int_i8 = int_i8_val.to_string(); print!("{}\n", str_from_int_i8); let int_u8_val:i8 = 60; let str_from_int_u8 = int_u8_val.to_string(); print!("{}\n", str_from_int_u8); let str_value = format!("{}{}", 2, 100); print!("{}", str_value); |
The codes generate the following output.
1 2 3 | 1 60 2100 |
Convert i16 and u16 Integers to String
Next, we have i16/u16 integers. These numeric types are 16-bit values – signed or unsigned.
1 2 3 4 5 6 7 8 9 10 | let int_i16_val:i16 = 1; let str_from_int_i16 = int_i16_val.to_string(); print!("{}\n", str_from_int_i16); let int_u16_val:i16 = 60; let str_from_int_u16 = int_u16_val.to_string(); print!("{}\n", str_from_int_u16); let str_value = format!("{}{}", 2, 100); print!("{}", str_value); |
When we run the codes, we get the following result.
1 2 3 | 1 60 2100 |
Convert i32 and u32 Integers to String
First, we try out i32/ u32 types. These integer values are 32-bit values and are either signed or unsigned.
1 2 3 4 5 6 7 8 9 10 | let int_i32_val:i32 = 1; let str_from_int_i32 = int_i32_val.to_string(); print!("{}\n", str_from_int_i32); let int_u32_val:i32 = 60; let str_from_int_u32 = int_u32_val.to_string(); print!("{}\n", str_from_int_u32); let str_value = format!("{}{}", 2, 100); print!("{}", str_value); |
We will get the following result when we run the codes.
1 2 3 | 1 60 2100 |
Convert i64 and u64 Integers to String
Most other programming languages can handle 64-bit values, and we know them mostly as long values. In Rust, we have the equivalent type – the i62/u64 types, which are 64-bit values.
1 2 3 4 5 6 7 8 9 10 | let int_i64_val:i64 = 1; let str_from_int_i64 = int_i64_val.to_string(); print!("{}\n", str_from_int_i64); let int_u64_val:i64 = 60; let str_from_int_u64 = int_u64_val.to_string(); print!("{}\n", str_from_int_u64); let str_value = format!("{}{}", 2, 100); print!("{}", str_value); |
The codes generate the following output.
1 2 3 | 1 60 2100 |
Convert i128 and u128 to String
Then, we have 128-bit integer types – these are i128/ u128.
1 2 3 4 5 6 7 8 9 10 | let int_i128_val:i128 = 1; let str_from_int_i128 = int_i128_val.to_string(); print!("{}\n", str_from_int_i128); let int_u128_val:i128 = 60; let str_from_int_u128 = int_u128_val.to_string(); print!("{}\n", str_from_int_u128); let str_value = format!("{}{}", 2, 100); print!("{}", str_value); |
The codes:
1 2 3 | 1 60 2100 |
Convert Rust isize and usize Integer to String
Lastly, we have the isize/ usize integer values. These are N-bit values – signed or unsigned. Moreover, these types depend on the computer the program is running on 64 bits for 64-bit architecture and 32 bits for 32-bit architecture machines.
1 2 3 4 5 6 7 8 9 10 | let int_isize_val:isize = 1; let str_from_int_isize = int_isize_val.to_string(); print!("{}\n", str_from_int_isize); let int_usize_val:isize = 60; let str_from_int_usize = int_usize_val.to_string(); print!("{}\n", str_from_int_usize); let str_value = format!("{}{}", 2, 100); print!("{}", str_value); |
Output:
1 2 3 | 1 60 2100 |
Although not integer types, Rust has f32 and f64 types for floating-point numbers. We could use the same way of converting these values to String in Rust. Consider the following codes.
1 2 | let my_f32:f32 = 100.44; println!("{}", my_f32.to_string()); |
If we don’t specify the floating-point type, Rust assumes the value is of type f64.
Tested with Rust 1.53.0.