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.
Converting a string to an integer in Rust turns a text string containing digits into a real number your program can use for math. It's like reading a number written on a piece of paper and putting it into a calculator so you can add or subtract it. You use this whenever a user types a number as text and your code needs to treat it as a value.