9951 explained code solutions for 126 technologies


rustHow do I convert a string into another type in Rust?


In Rust, you can convert a string into another type using the parse() method. This method takes a string and attempts to parse it into the desired type. For example, the following code will convert a string to an integer:

let my_string = "42";
let my_int = my_string.parse::<i32>().unwrap();

println!("{}", my_int);

Output example

42

The code above consists of the following parts:

  1. let my_string = "42"; - This declares a variable my_string and assigns it the value "42", which is a string.
  2. let my_int = my_string.parse::<i32>().unwrap(); - This calls the parse() method on the my_string variable, specifying the type i32 (an integer) as the desired output type. The unwrap() method is used to handle any errors that may occur during the parsing process.
  3. println!("{}", my_int); - This prints the value of the my_int variable to the console.

For more information, see the Rust documentation on the parse() method.

Edit this code on GitHub