How to Convert a String to an Integer in Rust

Convert a string to an integer in Rust using the parse method with error handling.

Use the parse() method on your string to convert it into an integer type like i32.

let number_str = "42";
let number: i32 = number_str.parse().expect("Not a number");

This attempts to parse the string; if it fails, expect panics with your custom message. Use unwrap() for a default panic message or handle the Result explicitly if you need graceful error handling.