What is the u32::from_str_radix() method in Rust?

Overview

u32 stands for unsigned integers that are 32-bit. We can use the from_str_radix() method of this integer type to convert a string or a character of any base or radix to base 10.

Syntax

u32::from_str_radix(string, radix)

Parameters

string: This is the string whose equivalent we want to get in base 10.

radix: This is the radix or base for the given string.

Return value

The value returned is a base 10 equivalent of the given string.

Example

fn main() {
// get the base ten equivalent of a value in any radix
println!("{:?}", u32::from_str_radix("B", 16)); // string in base 16
println!("{:?}", u32::from_str_radix("111", 2)); // string in base 2
println!("{:?}", u32::from_str_radix("34", 8)); // string in base 8
println!("{:?}", u32::from_str_radix("A3", 16)); // string in base 16
println!("{:?}", u32::from_str_radix("1001", 2)); // string in base 2
println!("{:?}", u32::from_str_radix("D5", 16)); // string in base 16
}

Explanation

  • Lines 3–8: We get and print the decimal equivalents of the values above using the u32::from_str_radix() method in Rust.

Free Resources